Skip to main content
Known Participant
May 9, 2014
Question

Getting my movie clip to jump and stay on stage

  • May 9, 2014
  • 2 replies
  • 975 views

For an assignment and just to let you know I am a newbie.

My stage size is 640x 480y

I have placed an image of a chicken on my stage over the background and (with help) I have moving clouds.

I have gotten to move my chicken up, down, left and right using the arrow keys.

I can get it to jump on its own but then I can't seem to assign it to a specific key.

I also can't seem to keep it on the stage.

import flash.display.*;

import flash.events.*;

         

         

          /* created a container containing an instance

             of my background image & used addChild to

             place it one the stage*/

var myBackground:BackGround = new BackGround();

    addChild(myBackground);

          // Moving clouds//

var myCloud:MyCloud;

          

         //Created a loop that pushes 6 clouds to the stage//

for (var i:Number = 0; i<6; i++)

{

    myCloud = new MyCloud();

      addChild(myCloud);

            

        /*Creates a random value varible containing

          Math.random() which returns a number 0 <=

          And <1 so,will never get 1*/

    var randomValue: Number = Math.random()*1;

   

        /*Creates a random collection of clouds drawn

          moving in various positions of the stage width.

          And the x & y Scale of the clouds are set randomly*/      

     myCloud.x = Math.random()*stage.width;

     myCloud.scaleX = myCloud.scaleY = randomValue;       

    

         /*Creates an event for the myCloud to listen out for

           function to get the MovieClip (all clouds) flyby when

           entering the frame*/

    myCloud.addEventListener(Event.ENTER_FRAME, flyBy);

    function flyBy(event:Event):void

{

          // Modular function for all the clouds to move//

    MovieClip(event.currentTarget).x +=6;

    if (event.target.x> stage.stageWidth)

    {

        event.target.x = 0;

    }

}

    addChild(myCloud);

}

// The Hen and Key Board functions //

var myHen:ChickHen = new ChickHen;

addChild(myHen);

//The x and y positioning co ordinates for the hen//

myHen.x = 230;

myHen.y = 350;

var distance:int = 100;

//leftmost position based upon boundary box

var leftArrow:int = (myHen.x - myHen.width /2) + 100;

//rightmost position based upon boundary box

var rightArrow:int = (myHen.x + myHen.width / 2) - 100;

//topmost position based upon boundary box

var upArrow:int = (myHen.y - myHen.height / 2) + 100;

/* Add keyboard listeners to the stage */

stage.addEventListener(KeyboardEvent.KEY_DOWN,move);

function move(event:KeyboardEvent):void

{

/* Use a switch to determine which key was pressed */

var key = event.keyCode;

switch(key)

{

case 39:

myHen.x +=distance;

break;

case 37:

myHen.x -=distance;

break;

case 38:

myHen.y -=distance;

break;

case 40:

myHen.y +=distance;

break;

}

}

I used the following method to make it jump  but I don't know how to assign this to a specific key:

stage.addEventListener(KeyboardEvent.KEY_DOWN, jump);

stage.addEventListener(KeyboardEvent.KEY_UP, land);

function jump (event:KeyboardEvent): void

{

  myHen.y -=120

}

function land (event:KeyboardEvent): void

{

  myHen.y +=120

}

This topic has been closed for replies.

2 replies

Inspiring
May 9, 2014

you used two different functions to listen for the same events:

stage.addEventListener(KeyboardEvent.KEY_DOWN,move);

stage.addEventListener(KeyboardEvent.KEY_DOWN, jump);


one will "override the other" (same goes for your down event)


rename your listening function to sth. neutral and imnclude the jump in a switch condition like you did for the move function


import flash.ui.keyboard


stage.addEventListener(KeyboardEvent.KEY_DOWN,doSth);


function doSth(e:Event):void{

  switch(e.keyCode){

    case Keyboard.SPACE:jump();break;

    case Keyboard.UP:moveUp();break

  }

}


"I also can't seem to keep it on the stage. "


call the following function inside an enterframe event:


function keepMovieClipOnStage(_chicken:MovieClip):void{

if(_chicken.x<0)

   _chicken.x=0;

if(_chicken.y<0)

   _chicken.y=0;

if(_chicken.x>stage.stageWidth)

  _chicken.x=stage.stageWidth;

if(_chicken.y>stage.stageHeight)

  _chicken.y=stage.stageHeight;

}

Known Participant
May 9, 2014

I apologize, what I was trying to say was that I know how to make it jump by using the following method:

stage.addEventListener(KeyboardEvent.KEY_DOWN, jump);

but instead I used the following so that I could use the keyboard function

stage.addEventListener(KeyboardEvent.KEY_DOWN,move);





Ned Murphy
Legend
May 9, 2014

In your jump and land functions, if you want to have specific keys result in the actions being processed then use conditionals to check if the key used is the desired one.

function jump (event:KeyboardEvent): void

{

   if(event.keyCode == Keyboard.UP){

         myHen.y -=120

  }

}


That example uses the up key.  If you wish to use another key then you can use:  trace(event.keyCode);

in the function just as a way of determining what the code is for different keys.

Known Participant
May 18, 2014

Hi Ned

First I would like to thank you for helping me, . I have sorted my previous issues out. I now have control of the hen, it stays on the stage, moves side to side and jumps. YAY. Ok I have now placed images (movieClips) above the hen. With the testHitObject function I have made the objects disappear when the hen collides with the image . My issue now is that I need the hen to select the images in order. So if she selects the wrong one first it won't disappear (it's a sequence game) Is there a tutorial or something like that, that can help me.

Ned Murphy
Legend
May 18, 2014

Just assign a property to each image and assign each a sequential value.  Create a counter that you use to check the current value and compare it to the value of the image when the hen hits it.  IF the value of the image equalsd the value of the counter then remove the image and increase the counter by 1 for the next image.