00:00
00:00
Newgrounds Background Image Theme

SpeakyDooman just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

need help with array

409 Views | 8 Replies
New Topic Respond to this Topic

need help with array 2013-10-27 16:35:50


Hello I am working on a flash game and I am almost finished with it . I just need to know how to remove a movie clip from an array when my character/ hero runs into it.

; <code
var FoodArray:Array = new Array();
var Ftimer:Timer = new Timer(999,1);

stage.addEventListener(Event.ENTER_FRAME, spawn2);

function spawn2(e:Event):void {

if(FoodArray.length<10)
{

Ftimer.addEventListener
(TimerEvent.TIMER, Foodtimer);

}
Ftimer.start();

}
function Foodtimer(evt:TimerEvent):void{

var food_mc:food = new food();
food_mc.x = Math.floor(Math.random()*(500+1));
food_mc.y = Math.floor(Math.random()*(625+1));
addChild(food_mc);
FoodArray.push(food_mc);
trace(FoodArray.length);

checkhit();
}

function checkhit2():void {

//step 1: make sure array exists
if(FoodArray!=null && FoodArray.length!=0)
{
//step 2: check all enemies against villain
for(var i:int = 0;i < FoodArray.length; i++)
{
//step 3: check for collision
if(char_mc.hitTestObject(FoodArray[i]))
{

}
}
}
>


Something about art?

lol excuse the hand

Response to need help with array 2013-10-27 18:02:05


Up at the top add another array, call it something like 'remove', and in the if statement in the for loop to check for collisions, add something like

removeChild(FoodArray[i]);
remove.push(i);

And then after the for and the if, add something like

while(remove.length>0)
{
    FoodArray.splice(remove[0],1);
    remove.shift();
}

If you have the splice in the for loop, it gets all messed up with indexes, but you can put it outside and then it'll work fine if you store it in another array and keep chopping off the first value until it's gone. You can still remove the display for it in the main collision detection statement, but not from the array.


BBS Signature

Response to need help with array 2013-10-27 18:03:03


A basic method:

Array.prototype.removeElement=function(el:*):void{
  for(var i:uint=0;i<this.length;i++){
    if(this[i]==el){
      this.splice(i,1);
      return;
    }
  }
}

An advanced method:

Array.prototype.removeElement=function(el:*):void{
  var ind:int=this.indexOf(el);
  if(ind!=-1)this.splice(ind,1);
}

The most advanced method I can think of:

//when adding an element to the array, set its index
element.index=array.length;
array.push(element);

//then removing the element becomes easy
array[element.index]=null;
//but then you need to check for null value in your loops
//otherwise you need to do this every time
Array.prototype.removeElement=function(el:*):void{
  this.splice(el.index);
  for(var i:uint=el.index;i<this.length;i++){
    this[i].index--;
  }
}
//which is no better than the rest of the methods, if even worse

If you still do not know how to solve your problem, consult your Flash teacher in class (accept failure).

Response to need help with array 2013-10-27 18:05:42


When you need to remove an element inside the array, do this:

removeChild(array[index]);
array.splice(index,1);
index--;

Response to need help with array 2013-10-27 18:25:50


I put in the code but still not removing the movie clip from the stage once the char runs into it ...


Something about art?

lol excuse the hand

Response to need help with array 2013-10-27 18:47:25


At 10/27/13 06:25 PM, evowolfdemon wrote: I put in the code but still not removing the movie clip from the stage once the char runs into it ...

Post your new code. The first response and the one before what you just posted are what you should use.

If you're just copying and pasting, of course it won't work. The second one belongs in a loop.

Response to need help with array 2013-10-27 19:03:35


this is what i did for the firstone

var remove:Array = new Array();
var FoodArray:Array = new Array();
var Ftimer:Timer = new Timer(999,1);

stage.addEventListener(Event.ENTER_FRAME, spawn2);

function spawn2(e:Event):void
{

if (FoodArray.length < 10)
{

Ftimer.addEventListener(TimerEvent.TIMER, Foodtimer);

}
Ftimer.start();

}
function Foodtimer(evt:TimerEvent):void
{

var food_mc:food = new food();
food_mc.x = Math.floor(Math.random() * (500 + 1));
food_mc.y = Math.floor(Math.random() * (625 + 1));
addChild(food_mc);
FoodArray.push(food_mc);
trace(FoodArray.length);

checkhit();
}

function checkhit2():void
{

//step 1: make sure array exists
if (FoodArray!=null && FoodArray.length!=0)
{
//step 2: check all enemies against villain
for (var i:int = 0; i < FoodArray.length; i++)
{
removeChild(FoodArray[i]);
}
remove.push(i);
{
//step 3: check for collision
if (char_mc.hitTestObject(FoodArray[i]))
{
while (remove.length>0)
FoodArray.splice(remove[0],1);
remove.shift();
}

}
}
}


Something about art?

lol excuse the hand

Response to need help with array 2013-10-27 19:55:24


At 10/27/13 07:03 PM, evowolfdemon wrote: remove.push(i);

Reread your code. This line is pushing a number into the array, not the object you want to remove. Push the object you want instead.

and don't pm me I was eating dinner

Response to need help with array 2013-10-28 00:47:45


ok I was able to read my book and make this code . I got my food to spawn and the character can run into it and collect it. I just need help getting it to respawn XD im prob just tired .... ?

here is the code

var FoodArray:Array = new Array(beans,corn);
var FoodOnstage:Array = new Array();
var FoodColected:int = 0;

for(var i:int = 0; i <30; i++){
var TakeFood = FoodArray[int(Math.random() * 2)];
var food:MovieClip = new TakeFood();
addChild(food);
food.x = Math.random() * stage.stageWidth;
food.y = Math.random() * stage.height;
food.speed = Math.random()* 15 + 5;
FoodOnstage.push(food);
}

stage.addEventListener(Event.ENTER_FRAME,GetFood);

function GetFood(e:Event):void{
for (var i:int = FoodOnstage.length-1; i > -1; i--){
var currentFood:MovieClip = FoodOnstage[i];
if (currentFood.hitTestObject(char_mc)){
FoodColected++;
removeChild(currentFood);
FoodOnstage.splice(i,1);
}

}
}

}


Something about art?

lol excuse the hand