I have created a flash whack-a-mole game, and coded the 9 different x and y cordinates the 4 moles can come up on. But I can't code it so that the moles can never have the same x and y cordinates.
I have tried this code: (Barney and mulvarp is two moles. .flytt(); is the command that gives the mole a new place to be.
if( Barney.x == mulvarp.x ){
Barney.flytt();
}
Barney.addEventListener(MouseEvent.CLICK, flyttb);
function flyttb(evt:MouseEvent){
barneyAu.play();
//returnFourPositionsF();
Barney.flytt();
teller-=4;
score.text=teller+"p";
barneyTimer.reset();
barneyTimer.start();
}
mulvarp.addEventListener(MouseEvent.CLICK, flyttm);
function flyttm(evt:MouseEvent){
squishRiktig.play();
mulvarp.flytt();
teller+=1;
score.text=teller+"p";
mulvarpTimer.reset();
mulvarpTimer.start();
}
Kone.addEventListener(MouseEvent.CLICK, flyttmk);
function flyttmk(evt:MouseEvent){
squishRiktig.play();
Kone.flytt();
teller+=2;
score.text=teller+"p";
koneTi开发者_开发百科mer.reset();
koneTimer.start();
}
Baby.addEventListener(MouseEvent.CLICK, flyttmb);
function flyttmb(evt:MouseEvent){
squishRiktig.play();
Baby.flytt();
teller+=3;
score.text=teller+"p";
babyTimer.reset();
babyTimer.start();
}
And this is the .flytt(); function I got inside all of the moles in a actionscript:
function flytt():void{
var flyttUt:int=Math.random() * 8;
if(flyttUt==0){
x=243,30;
y=171,65
}
if(flyttUt==1){
x=630,55;
y=170,25;
}
if(flyttUt==2){
x=999,85;
y=175,55;
}
if(flyttUt==3){
x=244,85;
y=363,85;
}
if(flyttUt==4){
x=632,75;
y=360,20;
}
if(flyttUt==5){
x=996,25;
y=359,50;
}
if(flyttUt==6){
x=228,45;
y=572,40;
}
if(flyttUt==7){
x=627,75;
y=570,95;
}
if(flyttUt==8){
x=650,60;
y=382,05;
}
}
I got an advice on maybe using this and call returnFourPositionsF();
but I am unsure on how to put all the moles into one, since they all have different timers, and I don't want them to change position even tho only one is changing.
var positionA:Array = [ [243.30,171.65],[630.55,170.25],[999.85,175.55],[244.85,363.85],[632.75,360.20],[996.25,359.50],[228.45,572.40],[627.75,57.95],[650.60,382.05] ];
function returnFourPositionsF():Array{
shuffle(positionA);
return [positionA[0],positionA[1],positionA[2],positionA[3]]
}
function shuffle(a:Array) {
var p:int;
var t:*;
var ivar:int;
for (ivar = a.length-1; ivar>=0; ivar--) {
p=Math.floor((ivar+1)*Math.random());
t = a[ivar];
a[ivar] = a[p];
a[p] = t;
}
}
Run your shuffled array through a Dictionary
like so.
import flash.utils.Dictionary;
var ar:Array = [[200, 100], [600, 400], [200, 100], [479, 239]];
var dict:Dictionary = new Dictionary();
for(var i:int=0;i<ar.length;i++)
{
dict[ar[i]] = ar[i];
}
var temp:Array = [];
for(var item in dict)
{
temp.push(item);
}
Dictionaries can't have duplicate keys, so each duplicate key is removed. From there, you should be able to use this logic to avoid duplicate positions.
精彩评论