Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Getting my movie clip to jump and stay on stage

New Here ,
May 09, 2014 May 09, 2014

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

}

TOPICS
ActionScript
876
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 09, 2014 May 09, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 17, 2014 May 17, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 18, 2014 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 19, 2014 May 19, 2014

I'm really new to this and Ive been trying all day, please could give me an example?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
May 19, 2014 May 19, 2014

what Ned meant was this:

since you are using MovieClip as class of your images you can simpy declare a dynamic property for them like:

image10.order = 1;

image5.order = 2;

...

then at the start of your game you declare a variable pointer, which will store the progress of the player:

//in the beginning the pointer points to the the image with the order-property 1

var pointer:int = 1;

then in your testHitObject function you check if the collision object has the right order like

if(pointer == collisonObject.order){

   collisonObject.visible = false;

   //increment pointer that the chicken will target then the next image in the order

   pointer++;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 19, 2014 May 19, 2014

Thank you for the reply,

I've tried your and Ned's suggestion and I know I'm doing something really stupid.... but I can't see it?

Now when I move the hen to the image it disappears. 

import flash.display.*;

import flash.events.*;

import flash.ui.Keyboard;

          /* Variables to contain and create instances of buttons, images and background*/

var myHen:ChickHen = new ChickHen();

var buttonPlay:playButton = new playButton();

var myBackground:BackGround = new BackGround();

var buttonHelp:help = new help();

var myBake: bake = new bake();

var myCut: cut = new cut();

var myMake: make = new make();

var myWater: water = new water();

var myPlant: plant = new plant();        

var pointer:int = 1;

addChild(myBackground); 

addChild(myHen); 

addChild(buttonPlay);

addChild(buttonHelp);

addChild(myBake);

addChild(myCut);

addChild(myMake);

addChild(myWater);

addChild(myPlant);

    

/*These MovieClips are transparent until the "play" button is pressed*/   

   

myBake.alpha =0;

myCut.alpha = 0;

myMake.alpha = 0;

myWater.alpha = 0;

myPlant.alpha = 0;

/*--The x and y positioning co ordinates of my MovieClips & buttons ---*/

myHen.x = 230;

myHen.y = 350;

myBake.x = 590;

myBake.y = 157;

myCut.x = 345;

myCut.y = 145;

myMake.x = 214;

myMake.y = 170;

myWater.x = 95;

myWater.y = 160;

myPlant.x = 466;

myPlant.y = 167;

buttonPlay.x = 190;

buttonPlay.y = 204;

buttonHelp.x = 446;

buttonHelp.y = 230;

    

myPlant.order = 1;

myWater.order = 2;

myCut.order = 3;

myMake.order = 4;

myBake.order = 5;

/*------Introduction Page------*/

/*--creating an EventListener to listen out for when the play button is pressed---*/     

buttonPlay.addEventListener(MouseEvent.CLICK,startGame);

     function startGame(event:MouseEvent):void

     {

        myBake.alpha =1;

        myCut.alpha = 1;

        myMake.alpha = 1;

        myWater.alpha =1;

        myPlant.alpha = 1;

        buttonPlay.alpha = 0;

        buttonHelp.alpha = 0;

       

     }

    

/* event listener has  been removed once the play button is transparent*/

    

     if (buttonPlay.alpha == 0)

     {

     buttonPlay.removeEventListener(MouseEvent.CLICK,startGame);

      }

  

     

          /* Variable to contain image of cloud*/

var myCloud:MyCloud;

          

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

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

    {

         myCloud = new MyCloud(); /*instance of cloud*/

        

            

        /*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);

   }

       

  

   /*---This EventListener and function is to keep the hen on the stage----*/

   myHen.addEventListener(Event.ENTER_FRAME,keepMovieClipOnStage);

    function keepMovieClipOnStage(event:Event):void

    {

 

      if(myHen.x<10)

    {

      myHen.x=100;

    }

     if(myHen.x>stage.stageWidth)

   

    {

      myHen.x=230;

    }

 

    }

var distance:int = 80;

   /*leftmost position based upon boundary box*/

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

   /*rightmost position based upon boundary box*/

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

   /*topmost position based upon boundary box*/

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

       /* 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;

        }

    }

   stage.addEventListener(KeyboardEvent.KEY_DOWN, jump);

   stage.addEventListener(KeyboardEvent.KEY_UP, land);

     function jump (event:KeyboardEvent): void

   {

     switch(event.keyCode)

    {

      case Keyboard.UP:

      myHen.y =120;

      myHen.x +=10;

    }

 

}         

function land (event:KeyboardEvent): void

  

     {

       myHen.y =350;

     }

   /*------ end hen and key board functions-----*/

    

    

myWater.addEventListener(Event.ENTER_FRAME, collision);

function collision (event:Event): void    

{

     (myHen.hitTestObject (myWater))

     if (pointer == myWater.order)

     {

         myWater.visible = false;

     }

        pointer++;

  

      } /*this is what it looks like*/

Screen Shot 2014-05-19 at 9.45.40 pm.png


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
May 19, 2014 May 19, 2014

"Now when I move the hen to the image it disappears."

I thought that was the intended effect?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 20, 2014 May 20, 2014

yes, but only when it jumps to it not when I move the hen under it.

I understand now, what Ned is saying. but how do I link the "image1.order" (or in my case myWater.order) to myWater MovieClip?



"Just assign a property to each image and assign each a sequential value."

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
May 20, 2014 May 20, 2014

1.It`s very important that you either copy/paste/ask about code examples that are posted.

I posted:

if(pointer == collisonObject.order){

  collisonObject.visible = false;

  //increment pointer that the chicken will target then the next image in the order

//inside the brackets->

  pointer++;

}


and you changed it to:

if (pointer == myWater.order)

    {

        myWater.visible = false;

    }

//outside the brackets->

pointer++;<-outside the brackets

what happens is: if you miss the first image on the first try you will never get a chance to hit it again, because the order var is incremented.

if the user gets an image wrong you should indicate it somehow (alert, sound etc.)

2.For your collision function to work you have to rewrite it like this:

function collision(event:Event):void

{

  

    if (myHen.hitTestObject(myWater))

    {

      

        if (pointer == myWater.order)

          

        {

          

            myWater.visible = false;

          

            pointer++;

          

        }

      

    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 20, 2014 May 20, 2014

I did copy and paste and it didn't work. I'll try and figure it out.

Thank you for your trouble.

Regards

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 20, 2014 May 20, 2014
LATEST

It works!!! I finally figured it out what I did wrong, thank you so much for your help. Sorry for the frustration.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
May 09, 2014 May 09, 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;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 09, 2014 May 09, 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);





Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines