|
-
More problems with sprite movement
I fixed my first problem, and my practise project is starting to actually be able to do something. I can move my man with aswd and shoot moving balls with space, but there is an issue with it. When I'm moving my man by holding down a key, and then press space to shoot off a ball, then movement halts and you have to up and down the key again, to make the man continue to move.
Here is the code:
Code:
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveMan);
function moveMan(e:KeyboardEvent):void
{
var character:String = String.fromCharCode(e.charCode);
var oldKeyCode:int;
//Sets "awsd" as controls to move sprite
if (e.keyCode == 87)
{
man_mc.y -= 5;
oldKeyCode = e.keyCode;
}
if (e.keyCode == 83)
{
man_mc.y += 5;
oldKeyCode = e.keyCode;
}
if (e.keyCode == 65)
{
man_mc.x -= 5;
oldKeyCode = e.keyCode;
}
if (e.keyCode == 68)
{
man_mc.x += 5;
oldKeyCode = e.keyCode;
}
if (e.keyCode == 32)
{
//Put a ball on the stage
var newBall:ball = new ball();
this.addChild(newBall);
newBall.x = man_mc.x;
newBall.y = man_mc.y + (man_mc.height / 2);
e.keyCode = oldKeyCode;
}
}
(There is also some code in the ball mc, that makes the ball fly sideways out of the screen when "fired")
As you might be able to see, I tried using another variable to store the old key code in, and switch it after the actions of a space press has occured.
So, can anyone tell me how to make the sprite move after another key has been pressed, without having to up and down the key again?
As always, all help is much appreciated.
-
rabid_Delineator
I would imagine you could set a flag for each key , for example , up , and when pressed that flag gets set to true , and until the KEY_UP event , for that particular key code occurs , you could indefinitely affect the sprites position on screen accordingly. Likewise , when the KEY_UP event has occured , check against the key code , and toggle the flag to false. Then in your enterframe event or however your doing movement , check againt which flags are true , and move the sprite.
-
Could you give me a code example of what you mean?
-
rabid_Delineator
something like this :
Code:
private var moveUp : Boolean = false;
private var moveDown : Boolean = false;
private var moveRight : Boolean = false;
private var moveLeft : Boolean = false;
private function init() : void {
addEventListener( KeyboardEvent.KEY_DOWN , setFlagOn );
addEventListener( KeyboardEvent.KEY_UP , setFlagOff );
addEvenListener( Event.ENTER_FRAME , checkFlags );
};
private function setFlagOn( evt : KeyboardEvent ) : void {
var character:String = String.fromCharCode(e.charCode);
var oldKeyCode:int;
//Sets "awsd" as controls to move sprite
if (e.keyCode == 87)
{
//man_mc.y -= 5;
moveUp = true;
oldKeyCode = e.keyCode;
}
if (e.keyCode == 83)
{
//man_mc.y += 5;
moveDown = true;
oldKeyCode = e.keyCode;
}
if (e.keyCode == 65)
{
//man_mc.x -= 5;
moveLeft = true;
oldKeyCode = e.keyCode;
}
if (e.keyCode == 68)
{
//man_mc.x += 5;
moveRight = true;
oldKeyCode = e.keyCode;
}
if (e.keyCode == 32)
{
//Put a ball on the stage
var newBall:ball = new ball();
this.addChild(newBall);
newBall.x = man_mc.x;
newBall.y = man_mc.y + (man_mc.height / 2);
e.keyCode = oldKeyCode;
}
};
private function setFlagOff( evt : KeyboardEvent ) : void {
var character:String = String.fromCharCode(e.charCode);
var oldKeyCode:int;
//Sets "awsd" as controls to move sprite
if (e.keyCode == 87)
{
//man_mc.y -= 5;
moveUp = false;
oldKeyCode = e.keyCode;
}
if (e.keyCode == 83)
{
//man_mc.y += 5;
moveDown = false;
oldKeyCode = e.keyCode;
}
if (e.keyCode == 65)
{
//man_mc.x -= 5;
moveLeft = false;
oldKeyCode = e.keyCode;
}
if (e.keyCode == 68)
{
//man_mc.x += 5;
moveRight = false;
oldKeyCode = e.keyCode;
}
};
private function checkFlags( evt : Event ) : void {
if( moveUp ) {
targetSprite.y -= 1;
} else if( moveDown ){
targetSprite.y += 1; {
else if( moveLeft ) {
targetSprite.x -= 1;
} else if( moveRight ) {
targetSprite.x += 1;
};
};
Last edited by AttackRabbit; 11-03-2009 at 02:56 PM.
-
I get that much, I was thinking more of how you where thinking about implementing it into the code.
I don't have movement on enter frame, movement is as you can see from the code. When a key is pressed, the listener tells the function to check which key was pressed.
The ball I shoot off has an enter frame listener, because it just shoot of when it's added to the stage, but I can't figure out how to use it on my dynamic sprite, the man.
-
rabid_Delineator
I just typed out exactly how to implement it into the code ? What specifically is not clear ?
-
I was writing while you edited. I'm at full work looking through your code now.
I actually thought you had seen my post already, and was crazy fast answering it with the edit
-
rabid_Delineator
Oh i gotcha , I was like what the heck. I cant pull over any further ! heh. let me know how it goes.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|