Skip to main content
Known Participant
April 1, 2012
Question

Linking currentTarget to math.random

  • April 1, 2012
  • 1 reply
  • 1486 views

I am designing a game in which the player can travel through doors when they are the same color. There are three doors on stage:"door1","door2" and "door3". Each door is an instance of the same symbol. The symbol contains six frames, 3 copies of the door in a different color and 3 copies of the door  in different colors with an icon of the player.. The frames are labeled "color1", "color2"  "color3" and also "player1","player2" and "player3".

I am attempting to write code that tells Flash to a)select a door at random and then select a random frame for the

randomly selected door to set the game for play. For instance, at startup, Flash randomly selects "door2"  and then

randomly selects "player2". So, the player's starting position will be on "door2" and the door will be red. After this, Flash

will select  the other door colors randomly from the frames labelled "color1","color2" or "color3".

I am attempting to do this with this code:

                var min:Number =1;

                var max:Number =3;

("door"+String(Math.floor(Math.random()*  (max - min) + min) ));

This randomly selects the door.

I would like this to randomly select the color of the randomly selected door.

(e.currentTarget).gotoAndStop("color"+String(Math.floor(Math.random() *  (max - min) + min);  ));

I am, of course, doing something incorrectly, but not quite sure what. I'm pretty sure currentTarget is the correct tag to use here.

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
April 1, 2012

if you don't want to ensure one of each door is displayed and one of each player is selected, you can use:

var randomDoor:MovieClip=doorF();

doorColorF(randomDoor);

function doorF():MovieClip{

return this["door"+randomF(1,3)];

}

function doorColorF(door:MovieClip):void{

door.frameNum=randomF(1,3);

door.gotoAndStop("color"+door.frameNum);

}

function randomF(n1:int,n2:int):int{

if(n1<=n2){

return n1+int((n2-n1)*Math.random());

} else {

return n2+int((n1-n2)*Math.random());

}

}

withertonAuthor
Known Participant
April 1, 2012

Thanks for the reply, but I'm a novice at AS3 and have almost no idea what that means! I am trying to make sure I move along at a pace where I'm sure I understand everything in my code, even if I'm not yet making it as efficient as possible. The example I posted in the orignal post is actually a streamlined example of code I will have to alter for some other areas of the game.

Is there anyway to do what I was trying to do with the "currentTarget" tag? Or something simple along those lines?

kglad
Community Expert
Community Expert
April 1, 2012

i'm not sure you can do what you want using currentTarget because i doubt you want to do what you stated you want.

i suspect you want your three doors on-stage to be one of three colors (corresponding to your 3 players) and you want all three colors to be displayed so each player has a door they can use, correct?  for example, you don't want all your doors to be the same color, correct?