Arrays?? with MCs

7 posts

Flag Post

Im wondering if this code will work:


var myCoins:Array = new Array();//create Array that I am adding a MC called coin to in another layer
var i:Number = 0;
do
{
if(myCoins[i].hitTest(ball.square))//When the certain coin on the stage hits the ball
{
myCoins[i]._visible = 0;//use visible to test if the collision works
}
}while(i<(myCoins.length-2));//I think this is right, I want to just run the loop from 0 to the number of coins out on the stage
if(i == (myCoins.length-1))//When the loops gets out I want to run it again
{
i = 0;
}

Thanks

 
Flag Post

You’re code needs some refinements/optimizations but the practice of storing MovieClip references in an array is pretty standard and expected for more intricate applications. So yes, you’re on the right track. I haven’t checked if there are errors in the code though.

 
Flag Post

ok thx, but yeah for some reason this isnt working for what I want it to do. Ill keep looking at it thought

 
Flag Post

ok, so I know that the collision code works, but how do you create a loop to check for collisions without looping forever and causing the script to abort??

 
Flag Post

Use a timer (ie setInterval) or the EnterFrame event.

 
Flag Post

ok so now I figured that part out, but I have one other thing going on. When I collect the coins( there are 5 for level 1), I am doing level++; and sending the var level to the setCoins function where I use a loop to place my coins on the screen. But whats happening is, the code sets the coins out on the stage but for some reason my code for collision detection only checks for 5 of the coins, when, if I am in level 2, there are really 10 coins on the stage. Ill upload the swf and give you the code:

checkCoins = function()
{
	
	for(i=0;i<totalCoins;i++)
	{
	if(myCoins[i].hitTest(_root.ball.square))
	{
		myCoins[i].removeMovieClip();
		_root.score+=500;
		numCoins--;
		if(numCoins == 0)
	{
		level++;
		setCoins(level);
	}
	}
	}
}

function setCoins(level:Number){
	totalCoins = (level*5);
	numCoins = totalCoins;
	do{
	coin = attachMovie("coin","newcoin"+j,this.getNextHighestDepth());
	j++;
	myCoins.push(coin);
	coin._x = (random(475)+20);
	coin._y = (random(475)+20);
	}while(j < totalCoins);
	
}

This is only a fraction of my code, but this is where I think the error is. Thx alot: down below is the link to the swf.

 
Flag Post

http://www.kongregate.com/games/shank8/test-for-rapid-fire