View Full Version : help with movement?
kingie94
09-27-2008, 06:02 PM
My character moves using the arrow keys but i would like it to be able to move more freely.
I am using this code:
on (keyPress "<Left>") {
this._x-=10
_root.spike._rotation = 180;
}
on (keyPress "<Right>") {
this._x+=10;
_root.spike._rotation = 360;
}
on (keyPress "<Up>") {
this._y-=10;
_root.spike._rotation = 270;
}
on (keyPress "<Down>") {
this._y+=10
_root.spike._rotation = 90;
}
If any1 can help it would be great thankyou :)
drumn4life0789
09-27-2008, 11:29 PM
more freely how
and when posting code please put it in a readable form.
kingie94
09-28-2008, 06:21 PM
more freely because when i press a key it takes a while for the character to move and if i press any other key it stops moving.
drumn4life0789
09-28-2008, 06:56 PM
do it like this instead.
var keyListener:Object = new Object();
keyListener.onKeyDown = function () {
onEnterFrame = function () {
if(Key.isDown(Key.RIGHT)) {
if(ball_mc._x>0 && ball_mc._x<550) {
ball_mc._x += ballSpeed;
}
}
else if(Key.isDown(Key.LEFT)) {
if (ball_mc._x>0 && ball_mc._x<550) {
ball_mc._x -= ballSpeed;
}
}
else if(Key.isDown(Key.UP)) {
if (ball_mc._y>0 && ball_mc._y<400) {
ball_mc._y -= ballSpeed;
}
}
else if(Key.isDown(Key.DOWN)) {
if (ball_mc._y>0 && ball_mc._y<400) {
ball_mc._y += ballSpeed;
}
}
}
}
Key.addListener(keyListener);
The Key.addListener(keyListener); at the bottom of the script could be placed up top with the part that is declaring it as a new object but I put it at the bottom to show that this is the end of this part of the script.
this is just some code I used when working on a simple game. You should be able to adapt it to your example but if you cant then please ask more questions Ill be glad to help.
Also if you take out the word else on all of the else if statements you will be able to hit up and right at the same time and make your object move at an angle.
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.