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

random move clip

Explorer ,
Jan 07, 2019 Jan 07, 2019

Copy link to clipboard

Copied

help plz .. i want  loop random  function make the apples  drop like rain from up to down ..and attach it with my code down plz help

plz be  attention that the apples  don't go outside the stage when it dropped

the instance name of apples move clips :apple1 apple2 apple3

thumbnail_IMG_3494.jpg

my code :

stop();

  function mouseMoveHandler(e:Event):void 

    girlMc.x = mouseX; 

 

    if (girlMc.x < 0) 

          girlMc.x = 0; 

    else if (girlMc.x > stage.stageWidth - girlMc.width) 

          girlMc.x = stage.stageWidth - girlMc.width; 

 

stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);

Views

255

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Expert , Jan 08, 2019 Jan 08, 2019

Hi.

Here is a sample for you:

AS3 code:

// advanced layers off

// tested in Animate CC 2019 (version 19.1, build 349)

import flash.events.Event;

import flash.utils.setInterval;

import flash.utils.clearInterval;

var AppleClasses:Array = [Apple, Apple1, Apple2]; // linkage names from the Library

var totalApples:uint = 10; // total amount of apples that will be reused

var yRange:uint = 1000; // maximum y position above the stage everytime an apple is created or respawn

var minSpeedY:uint = 2;

var maxSpeedY:uin

...

Votes

Translate

Translate
Community Expert ,
Jan 08, 2019 Jan 08, 2019

Copy link to clipboard

Copied

LATEST

Hi.

Here is a sample for you:

AS3 code:

// advanced layers off

// tested in Animate CC 2019 (version 19.1, build 349)

import flash.events.Event;

import flash.utils.setInterval;

import flash.utils.clearInterval;

var AppleClasses:Array = [Apple, Apple1, Apple2]; // linkage names from the Library

var totalApples:uint = 10; // total amount of apples that will be reused

var yRange:uint = 1000; // maximum y position above the stage everytime an apple is created or respawn

var minSpeedY:uint = 2;

var maxSpeedY:uint = 7;

var score:uint = 0;

var appleCreationDelay:uint = 1; // each apple will be create with this delay in seconds in the beginning until the total amount of apples set above is reached

var applesInterval:uint = 0;

var apples:Array;

function enterFrameHandler(e:Event):void

{

     for (var i:int = apples.length - 1; i >= 0; i--)

     {

          var apple:* = apples;

          apple.y += apple.speedY;

          if (apple.hitTestObject(girlMc)) // here we check if some apple hits the girl

          {

               score = Math.min(++score, uint.MAX_VALUE);

               setScore();

               randomValues(apple);

          }

          else if (apple.y > stage.stageHeight) // here we check if some apple reaches the bottom

          {

               score = Math.max(--score, 0);

               setScore();

               randomValues(apple);

          }

     }

     girlMc.x = mouseX; // move the girl on x axis according to the mouse cursor

     // limit the girl within the stage

     if (girlMc.x < 0)

          girlMc.x = 0;

     else if (girlMc.x > stage.stageWidth - girlMc.width)

          girlMc.x = stage.stageWidth - girlMc.width;

}

function createApple():void

{

     var apple:* = new AppleClasses[uint(Math.random() * AppleClasses.length)]();

     randomValues(apple);

     applesContainer.addChild(apple);

     apples.push(apple);

}

function setScore():void

{

     scoreText.text = "SCORE: " + score;

}

function randomValues(target:*):void

{

     target.x = uint(randomRange(0, stage.stageWidth - target.width));

     target.y = int(randomRange(-Math.random() * yRange, -target.height));

     target.speedY = uint(randomRange(minSpeedY, maxSpeedY));

}

function randomRange(min:Number, max:Number):Number

{

     return min + Math.random() * (max - min);

}

function start():void

{

     apples = [];

     createApple();

     applesInterval = setInterval(function()

     {

          if (apples.length <= totalApples)

               createApple();

          else

               clearInterval(applesInterval);

     }, appleCreationDelay * 1000);

     stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);

}

start();

Please notice that if we are going to have to use an enterFrame event, then we don't need that mouseMove event anymore.

FLA download:

animate_cc_as3_follow_cursor_inside_of_the_stage.zip - Google Drive

I hope this helps.

Regards,

JC

Votes

Translate

Translate

Report

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