...

View Full Version : controlling objects help



laylo
04-05-2012, 12:30 PM
basically, i got this space game to make and i've created a ship, then exported it for ActionScript. this is the code inside the ship.

package {


import flash.display.MovieClip;
import flash.events.Event;


public class mainShip extends MovieClip
{


public function mainShip()
{
stage.addEventListener(Event.ENTER_FRAME, keyPressed);
function keyPressed(event:Event):void
{
switch (this.ship)
{
case key.isDown.Keyboard.RIGHT :
x += 5;
break;

default :
break;
}
}
}


}
}

i got an error undefined property of key. any suggestions?

adaminaudio
04-05-2012, 04:44 PM
hi,

have you tried using a keyboard event listener as opposed to an enter frame event?



stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);

...

//pass a keyboard event object
function onKeyPressed(event:KeyboardEvent):void
{
if(event.keycode == Keyboard.RIGHT)
{

//do your stuff here...
}

}

laylo
04-06-2012, 12:39 PM
yeah cool! i can move my ship now but the problem now is that I think i needed the frame event to add velocity to it.

Is there a way to use the keyboard arrows but have an enter frame event at the same time??


stage.addEventListener(KeyboardEvent.KEY_DOWN, moveShip);
function moveShip(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
ship.x -= speed;
}
else if (event.keyCode == Keyboard.RIGHT)
{
ship.x += speed;
}

if (event.keyCode == Keyboard.UP)
{
ship.y -= speed;
}
else if (event.keyCode == Keyboard.DOWN)
{
ship.y += speed;
}

} //end of moveShip function



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum