Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts

    controlling objects help

    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?

  • #2
    New Coder
    Join Date
    Jan 2012
    Location
    Columbus, Ohio, U.S.A
    Posts
    41
    Thanks
    0
    Thanked 8 Times in 8 Posts
    hi,

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


    Code:
    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...
       }
    
    }

  • #3
    New Coder
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts
    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??
    Code:
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •