actionscript 3 - as3 applying gravity to a missile -


i'm trying create "worms" style turn based artillery game as3. able add ball on x/y location of character , have ball fire in direction of mouses location when click made having difficulty in applying gravity ball. if can me @ need make ball drop in arc after being fired great.

my code adding ball , having fire in selected direction is: game main:

function redfollow(e:mouseevent):void {

        var a1 = mousey - redpower.y;         var b1 = mousex - redpower.x;         var radians1 = math.atan2(a1,b1);         var degrees1 = radians1 / (math.pi/180);         redpower.rotation = degrees1;       }       public function redfire(event:mouseevent)     {          removechild(redpower);         turnchangeover();         addchild(rpball);         trace("fire!");         rpball.x = red.x;         rpball.y = red.y;         rpball.rotation = redpower.rotation + 90;      } 

and ball's class code is:

package {

import flash.display.movieclip; import flash.events.event;  public class redball extends movieclip {       public function redball() {         this.addeventlistener(event.enter_frame, moveshot);     }   function moveshot(event:event){         var thismissile:redball = redball(event.target);         var _missilespeed:int = 7;         var gravity:int = 0.8;           //get x,y components of angle         var missileangleradians = ((-thismissile.rotation - 180) * math.pi /180);         //trace("missle angle: " + missileangleradians);          var ymoveincrement = math.cos(missileangleradians) * _missilespeed;         var xmoveincrement = math.sin(missileangleradians) * _missilespeed;             thismissile.y = thismissile.y +ymoveincrement;           thismissile.x = thismissile.x + xmoveincrement;          trace(thismissile.y);    }    } 

}

you need store speed separately x , y. see, after ball has been launched, don't care angle, care speed, , speed what's affected gravity. split speed x , y components in ymoveincrement,xmoveincrement, need have them properties of ball (it'll velocity), , add small per-frame increment y component of velocity.

public class redball extends movieclip {      public var yvelocity:number=0;     public var xvelocity:number=0; // these assigned when ball shot     public static var gravity:number=0.5; // number, not "int"     // "static" it'll affect balls     public function redball() {         this.addeventlistener(event.enter_frame, moveshot);     }     function moveshot(event:event){         // var thismissile:redball = redball(event.target);         // why ^ this? can use "this" qualifier in function         // have x,y components of angle         this.y = this.y + this.yvelocity;         this.x = this.x + this.xvelocity;         this.yvelocity = this.yvelocity + gravity;          // also, care rotation - let's add trick         var angle:number=math.atan2(this.yvelocity,this.xvelocity);          // direction angle, in radians         this.rotation=angle*180/math.pi; // make degrees , rotate          trace(thismissile.y);     } } 

it'll practice if add shoot function redball class accept angle , initial velocity, assign xvelocity , yvelocity did in listener.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -