Reputation: 31
I have generated random bubbles, I used a code I found in the net. Now I want a click event that will hide a random bubble.
Here is exactly the code I used,
http://good-tutorials-4u.blogspot.com/2009/04/flash-bubbles-with-actionscript-3.html
I got the bubbles running good...
I have tried this, and so far no luck..
addEventListener(MouseEvent.CLICK, eventListener);
function eventListener(eventObject:MouseEvent) {
bubbles[i].splice(i,1,bubbles[i]);
}
I tried using an array but it returns me this output TypeError: Error #2007: Parameter child must be non-null. at flash.display::DisplayObjectContainer/removeChild() at Function/()
TypeError: Error #2007: Parameter child must be non-null. at flash.display::DisplayObjectContainer/removeChild() at Function/()
Upvotes: 2
Views: 190
Reputation: 1370
try this
bubbles.addEventListener(MouseEvent.CLICK, eventListener); // place this listener in moveBubbles function.
function eventListener(eventObject:MouseEvent):void {
eventObject.currentTarget.visible = false;
}
Upvotes: 0
Reputation: 19748
Just a minor revision on Baris Usakli's code here, this is if you want the one that was clicked on to be removed.
var bubbles:Array = [];
function makeBubbles()
{
for(var i:int=0;i<100;i++)
{
var bubble:Bubble = new Bubble();
bubbles.push(bubble);
addChild(bubble);
bubble.addEventListener(MouseEvent.CLICK, eventListener);
}
}
function eventListener(eventObject:MouseEvent) {
var clickedBubbleIndex:int = bubbles.indexOf(eventObject.currentTarget);
parent.removeChild(eventObject.currentTarget);
bubbles.splice(clickedBubbleIndex:int, 1);
}
Upvotes: 0
Reputation: 13532
If you have the bubbles in an array this should work.
var randomIndex:int = int(Math.random() * bubbles.length);
parent.removeChild(bubbles[randomIndex]);
bubbles.splice(randomIndex, 1);
Notice that you have to remove the bubble from the display list too.
Upvotes: 2
Reputation: 1231
You can try creating a new array without a random element from the original array. Then just reassign the old array to the new one, e.g.
// get the random index to remove element at
var randomIndex:int = 0 + bubbles.length * Math.random();
var index:int = 0;
// create new array containing all apart from the chosen one
var newBubbles:Array = [];
for each (var item:Object in bubbles) {
if (index != randomIndex) {
newBubbles.push(item);
}
index++;
}
// here you go new array without one random item
bubbles = newBubbles;
Or something like these.
Upvotes: 0