Getting my movie clip to jump and stay on stage
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
}
