View Full Version : Rotation code not working?
sciguy77
August 20th, 2009, 12:27 AM
Hi,
With this code my character can move forward fine, but cannot rotate left or right for some reason.
hero.gotoAndStop('still');
var Key:KeyObject = new KeyObject(stage);
stage.addEventListener(Event.ENTER_FRAME,onenter);
function onenter(e:Event):void{
if(Key.isDown(Key.UP)){
hero.y-=5;
hero.scaleX=1;
hero.gotoAndStop('walking');
}else{
hero.gotoAndStop('still');
}
}
function onenter2(e:Event):void{
if(Key.isDown(Key.LEFT)){
hero.rotate -= 5;
hero.scaleX=1;
}
}
function onenter3(e:Event):void{
if(Key.isDown(Key.RIGHT)){
hero.rotate += 5;
hero.scaleX=1;
}
}
IQAndreas
August 20th, 2009, 12:53 AM
Try:
hero.rotation += 5;
sciguy77
August 20th, 2009, 01:20 AM
Nope, didn't work. :(
anakadote
August 20th, 2009, 01:38 AM
First, it should be "rotation", not "rotate". Second, you have an event listener for adjusting the "y" value of hero, but not the rotation. You should put the code for the rotation in the same function, onenter, as opposed to registering three different listeners for enter_frame events:
hero.gotoAndStop('still');
var Key:KeyObject = new KeyObject(stage);
stage.addEventListener(Event.ENTER_FRAME, onenter);
function onenter(e:Event):void{
if(Key.isDown(Key.UP)){
hero.y-=5;
hero.scaleX=1;
hero.gotoAndStop('walking');
} else if(Key.isDown(Key.LEFT)){
hero.rotation -= 5;
hero.scaleX=1;
} else if(Key.isDown(Key.RIGHT)){
hero.rotation += 5;
hero.scaleX=1;
} else {
hero.gotoAndStop('still');
}
}
anakadote
August 20th, 2009, 01:42 AM
oh, and instead of using an enter_frame event listener, you could listen for a key to be pressed:
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyClick);
sciguy77
August 20th, 2009, 02:07 AM
Ok, now the hero can rotate, but there's a new problem. I need the hero to move in the direction its pointing, so if it turns right it can walk in that direction.
Thanks
dillontrujillo
August 20th, 2009, 02:12 AM
i think now it work properly he is right.
Acne Treatment (http://www.goarticles.com/cgi-bin/showa.cgi?C=1839461)
sciguy77
August 20th, 2009, 02:16 AM
Um, no. I still need it to move forward when it rotates.
anakadote
August 20th, 2009, 02:24 AM
Um, no. I still need it to move forward when it rotates.
Then increase its x position at the same time you alter its rotation:
hero.x += 5;
sciguy77
August 20th, 2009, 02:37 AM
No, I mean say the player rotates the hero left a little and then moves forward. The hero will move up, not forward.
IQAndreas
August 21st, 2009, 03:34 AM
For moving the hero upward, use
hero.y -= 5;
But it might be different depending on your code.
If you post a little screenshot with a few arrows of what you want, I can provide the right code.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.