John Tolman
John Tolman

Reputation: 1

Random animation placement in flash

I'm looking for a way to have an animation play when clicked to move on to the next screen. But for it to be randomly generated to a different place on the stage with a new animation. When clicking a spot it is not in, it plays a different animation but doesn't advance to the next part of my project.

Example, there are six black boxes, you click on the first black box, a red ball comes up animates and moves on to the next scene. The next time you go through the project, you click on the first box but instead a green ball animates. It does not move on to the next scene. but the red ball will be behind one of the other 5 boxes. Once found the red ball will do the animation and move on to the next scene.

If anyone can figure out my riddle, I greatly appreciate it. thank you.

Upvotes: 0

Views: 331

Answers (2)

Jason Sturges
Jason Sturges

Reputation: 15955

If you had 6-boxes laid out, each with an instance name of box1, box2, box3, etc...

grid

And given you had symbols in the library defined as:

library

You could implement in actions:

import flash.events.MouseEvent;
import flash.display.MovieClip;

// determine which box moves forward
var targetBox:uint = Math.ceil(Math.random() * 6);
trace("looking for box: " + targetBox);

// add mouse click event listeners to the boxes
box1.addEventListener(MouseEvent.CLICK, boxClickHandler);
box2.addEventListener(MouseEvent.CLICK, boxClickHandler);
box3.addEventListener(MouseEvent.CLICK, boxClickHandler);
box4.addEventListener(MouseEvent.CLICK, boxClickHandler);
box5.addEventListener(MouseEvent.CLICK, boxClickHandler);
box6.addEventListener(MouseEvent.CLICK, boxClickHandler);

// add "targetId" property to each movie clip
box1.targetId = 1;
box2.targetId = 2;
box3.targetId = 3;
box4.targetId = 4;
box5.targetId = 5;
box6.targetId = 6;

function boxClickHandler(event:MouseEvent):void
{
    // get the movie clip of box clicked:
    var clickedBox:MovieClip = event.target as MovieClip;

    // remove mouse click for this box in the future
    clickedBox.removeEventListener(MouseEvent.CLICK, boxClickHandler);

    // if clicked box targetId matches our target Box call
    // boxFound function; otherwise, otherwise, wrong target.
    if(targetBox == clickedBox.targetId)
        boxFound(clickedBox);
    else
        wrongBox(clickedBox);
}


function boxFound(clickedBox:MovieClip):void
{
    // add red ball
    var redBall:RedBall = new RedBall();
    addChild(redBall);

    redBall.x = clickedBox.x;
    redBall.y = clickedBox.y;

    // go to next frame after animation completes...
}

function wrongBox(clickedBox:MovieClip):void
{
    // add green ball
    var greenBall:GreenBall = new GreenBall();
    addChild(greenBall);

    greenBall.x = clickedBox.x;
    greenBall.y = clickedBox.y;
}

Which gives you this:

game play

Download Flash Pro CS 5 FLA example source code here.

View SWF example here.

Upvotes: 3

puggsoy
puggsoy

Reputation: 1280

What I understand is that you want the user to find the red ball behind one of the black boxes, but each time you play the red ball is behind a different, randomly chosen box, correct? In that case, you could do something like this:

The five boxes are stored in an array. To choose which box the ball goes behind, do something like this:

ballBox:int = Math.random() * (4 + 1);

That algorithm will give a random number between 4 and 0, so ballBox can then be used to access a random box in the array, like this:

boxArray[ballBox];

That's the gist of how to get a random box. The rest is just putting the ball behind that box.

Upvotes: 0

Related Questions