actionscript 3 - Character movement tied to keypress, increases animation speed with increased character speed -
i've come across tough problem (at least me) can't figure out. have stick figure, , want make simple running animation depending on if moving left or right, tie animation speed how fast in x direction moving.
below included code how character moves (all unncessary code game removed). xspeed
want animation speed somehow linked too. relate absolute value of xspeed
since can negative. ideally, i'd have 2 animations, 1 moving left , 1 moving right. thought making both animations on same timeline stickman1, doing this.
if (xspeed > 0){stickman1.gotoandplay(2)} if (xspeed < 0){stickman1.gotoandplay(5)}
assuming animation going right 3 frames long, beginning on frame 2, ending on 4, , animation going left 3 frames long, beginning on frame 5, ending on 7, , on frame 4 , 7 putting in code says gotoandplay(correct frame repeat)
. said though, know it's bad practice coding on timeline, if possible stay away that. gets worse. have no idea how speed animation =(. that's at, concerning these problems appreciated, , full code of character movement below! thanks!
public var gametimer:timer; public var stickman1:stickman1; public var leftbool:boolean = false; public var rightbool:boolean = false; public var accel:number = 0.5; public var maxspeed:number = 8; public var xspeed:number = 0; public function gamescreen():void { this.addeventlistener(event.enter_frame, addsomelisteners, false, 0, true); stickman1 = new stickman1(); stickman1.x = 250; stickman1.y = 300; addchild(stickman1); gametimer.addeventlistener(timerevent.timer, ontick, false, 0, true); gametimer = new timer(25); gametimer.start(); } public function addsomelisteners(event:event):void { stage.addeventlistener(keyboardevent.key_down, onkeydown, false, 0, true); stage.addeventlistener(keyboardevent.key_up, onkeyup, false, 0, true); } public function ontick(timerevent.timerevent):void { if(rightbool==true && xspeed<maxspeed){xspeed+=2} if(leftbool==true && xspeed>-maxspeed){xspeed-=2} if(xspeed>0){xspeed-=accel} if(xspeed<0){xspeed+=accel} stickman1.x+=xspeed; stickman1.y+=yspeed; } public function onkeydown(keyboardevent.keyboardevent):void { if (event.keycode == keyboard.left){leftbool = true}; if (event.keycode == keyboard.right){rightbool = true}; } public function onkeyup(keyboardevent.keyboardevent):void { if (event.keycode == keyboard.left){leftbool = false}; if (event.keycode == keyboard.right){rightbool = false}; }
you'll need manage own animation system, it's not hard sounds :)
something should work (assuming frame 0 standing, , frame 1-n running):
public var dir:int = 0; // direction you're going; -1 = left, 1 = right, 0 = not moving public var speed:number = 0.0; // speed of char public var stickman:stickman; // stickman obj public var prevtime:number = 0.0; // used calculate delta time in update public var curranimtime:number = 0.0; // our current animation time public var curranimframe:int = 0; // our current animation frame public function gamescreen():void { // create our stickman this.stickman = new stickman(); this.stickman.gotoandstop( 0 ); // stand frame … // add our listeners this.addeventlistener( event.enter_frame, this._onenterframe ); this.addeventlistener( keyboardevent.key_down, this._onkeydown ); this.addeventlistener( keyboardevent.key_up, this._onkeyup ); this.prevtime = gettimer(); } private function _onenterframe( e:event ):void { // calculate delta time var currtime:int = gettimer(); var dt:number = ( currtime - this.prevtime ) * 0.001; // dt in seconds this.prevtime = currtime; // check if we're slowing down if( this.dir == 0 ) { this.speed *= 0.8; if( this.speed <= 0.01 ) // or whatever value want this.speed = 0.0; } else { // we're running this.speed += 20 * dt * this.dir; // or whatever speed increase want if( this.speed > 20 ) this.speed = 20; // max speed; } // if our speed 0, play stand anim , return if( this.speed == 0 ) { this.gotoandstop( 0 ); return; } // our anim time - seconds frame. // basically, if speed 20, our anim time // 1.0 (1 second) / 20.0 -> 0.05, or 0.05 seconds // per frame. use seconds because dt , curranimtime // in seconds. note: linear transform of speed -> fps var animtime = 1.0 / this.speed; // update our current anim time this.curranimtime += dt; while( this.curranimtime > animtime ) { // increase our current anim frame this.curranimframe++; if( this.curranimframe > this.totalframes ) this.curranimframe = 1; // frame 0 our stand anim } // go our new frame if( this.curranimframe != this.currframe ) this.gotoandstop( this.curranimframe ); // flip our scale based on if we're right or left this.scalex = ( this.dir < 0 ) ? -1 : 1; } private function _onkeydown( e:keyboardevent ):void { // move left or right if( e.keycode == keyboard.left ) this.dir = -1; else if( e.keycode == keyboard.right ) this.dir = 1; } private function _onkeyup( e:keyboardevent ):void { // stop if( e.keycode == keyboard.left || e.keycode == keyboard.right ) this.dir = 0; }
basically change time show each frame for, , manually handle changing right frame. way, animation speeds , slows down speed of char.
Comments
Post a Comment