Newgrounds.com — Everything, By Everyone.

Checking login status…

USERNAME:

PASSWORD:

Logging in…

Logged in as:
.
Logging out…
Inbox My Account Log Out


Forum Topic: How can I make an array that loops?

(158 views • 16 replies)

This topic is 1 page long.

<< < > >>
Questioning

Petwoip

Reply To Post Reply & Quote

Posted at: 5/13/08 09:12 PM

Petwoip LIGHT LEVEL 04

Sign-Up: 01/18/05

Posts: 227

I need an array to loop a series of objects that are all duplicated movie clips. How can I set something like this up?

I'm trying to learn arrays but I cannot think of how to manipulate them in such a way as I seek.

.

BBS Signature

None

gorman2001

Reply To Post Reply & Quote

Posted at: 5/13/08 09:40 PM

gorman2001 NEUTRAL LEVEL 14

Sign-Up: 08/18/02

Posts: 1,850

i dont know what u mean... loop an array while trying to read it ? like if your trying to access a value past the last one in an array, return the first one ?


None

LCurtis

Reply To Post Reply & Quote

Posted at: 5/13/08 09:44 PM

LCurtis NEUTRAL LEVEL 17

Sign-Up: 05/23/07

Posts: 1,075

An array works like this:

var array:Array = new Array(4); // creates an array with four spots
array[0] = value;
array[1] = value;
array[2] = value;
array[3] = value;

A for loop works like this:
for(var i=0; i<4; i++)
{
trace(i);
}
// Outputs: 0,1,2,3

So from this we see that we can use a for loop to make a variable go from 0-3. So try and combine this too and see if you can loop through all the spots in the array and give them a value.

support the dream ...

BBS Signature

None

Petwoip

Reply To Post Reply & Quote

Posted at: 5/13/08 10:07 PM

Petwoip LIGHT LEVEL 04

Sign-Up: 01/18/05

Posts: 227

Ok, I understand what you are saying, but I still have a few questions. I need this array to go through all of the duplicated movie clips. However, the number of these movie clips is indefinite as they are constantly being created during gameplay.

Do I have do this:

//Action happens (a duplicate is created)
for(i=0; i< (what would i put here if the number is indefinite?); i++){
Array[i] = _root.["movieclip" + i]
i += 1
}

So is this a proper set-up? I have that one problem in the for loop, but with this stores all the values in the array for every duplicated MC. Also, how would I be able to incorporate a hitTest with all of the objects of the array? Please respond, I appreciate the help.

.

BBS Signature

None

gorman2001

Reply To Post Reply & Quote

Posted at: 5/13/08 10:11 PM

gorman2001 NEUTRAL LEVEL 14

Sign-Up: 08/18/02

Posts: 1,850

create a global variable that would be the count of the movieclips

meaning in your code, each time a movieclip is added, increment by 1 the variable's value.

and in your loop, put the count variable as your loop limit.


Questioning

Petwoip

Reply To Post Reply & Quote

Posted at: 5/13/08 10:23 PM

Petwoip LIGHT LEVEL 04

Sign-Up: 01/18/05

Posts: 227

Wouldn't that variable you are talking about be i, since i is the count of duplications?

.

BBS Signature

Questioning

Petwoip

Reply To Post Reply & Quote

Posted at: 5/13/08 10:24 PM

Petwoip LIGHT LEVEL 04

Sign-Up: 01/18/05

Posts: 227

Sorry for the double post. My main worry is not the gloabal variable part, but more whether the code I wrote can work...

and also how can I make a hitTest with all the objects inside the array.

.

BBS Signature

None

LCurtis

Reply To Post Reply & Quote

Posted at: 5/13/08 10:25 PM

LCurtis NEUTRAL LEVEL 17

Sign-Up: 05/23/07

Posts: 1,075

At 5/13/08 10:11 PM, gorman2001 wrote: create a global variable that would be the count of the movieclips

Sorta. Every array has a property called length. This property gives you how many items are currently in the array.

In the for loop you dont put i +=1;
In the header of the for loop there is i++ which automatically increments i by 1 every time it loops through.

To your question about hitTests, when you call array[number] it gives you the object you stored there.
Lets say you store a movieclip called myMC into spot 3 of myArray.
Then myArray[2].hitTest() and myMC.hitTest() are the same.

Do you create your movieclips in a for loop?
If you do then you can do:
myArray = new Array();
for(...){
myArray.push(_root.attachMovie(...));
}
When you call the attachMovie function it "returns" a reference to the newly added MovieClip. As you get more into programming you will understand more about functions and returns but for now just know that line adds the newly attached MC to the end of the array.

support the dream ...

BBS Signature

None

Petwoip

Reply To Post Reply & Quote

Posted at: 5/13/08 10:31 PM

Petwoip LIGHT LEVEL 04

Sign-Up: 01/18/05

Posts: 227

At 5/13/08 10:25 PM, LCurtis wrote:
At 5/13/08 10:11 PM, gorman2001 wrote: create a global variable that would be the count of the movieclips
Sorta. Every array has a property called length. This property gives you how many items are currently in the array.

In the for loop you dont put i +=1;
In the header of the for loop there is i++ which automatically increments i by 1 every time it loops through.

To your question about hitTests, when you call array[number] it gives you the object you stored there.
Lets say you store a movieclip called myMC into spot 3 of myArray.
Then myArray[2].hitTest() and myMC.hitTest() are the same.

Do you create your movieclips in a for loop?
If you do then you can do:
myArray = new Array();
for(...){
myArray.push(_root.attachMovie(...));
}
When you call the attachMovie function it "returns" a reference to the newly added MovieClip. As you get more into programming you will understand more about functions and returns but for now just know that line adds the newly attached MC to the end of the array.

OK, i like the bit about the attach movie clip in the for loop. One question though that you may have misinterpreted is the hitTest. You showed me how to hitTest any member of the array, but how can I get a hitTest to work for all members of the Array?

.

BBS Signature

None

LCurtis

Reply To Post Reply & Quote

Posted at: 5/13/08 10:57 PM

LCurtis NEUTRAL LEVEL 17

Sign-Up: 05/23/07

Posts: 1,075

You have to loop through them. Use a for loop.
If you want them to hit test against each other then you use 2 for loops like so:

for(q=0; q<array.length; q++)
{
 for(w=0; w<array.length; w++)
 {
  if(array[q].hitTest(array[w])){...};
 }
}

support the dream ...

BBS Signature

None

Petwoip

Reply To Post Reply & Quote

Posted at: 5/13/08 11:26 PM

Petwoip LIGHT LEVEL 04

Sign-Up: 01/18/05

Posts: 227

No, what I meant was how can I detect if an outside object has hit any of the members of the array

.

BBS Signature

None

CaptinChu

Reply To Post Reply & Quote

Posted at: 5/13/08 11:41 PM

CaptinChu DARK LEVEL 15

Sign-Up: 09/11/05

Posts: 3,187

As the objects are created in actionscript, push them into an array. You have that for loop, so just add:

mvarr.push(movieclip);

If you loop through that array, you should be able to alter every item in the array in enough time to render a frame. I came through the same problem when I was making a shooting type game.

What I did was add the bullets to the array, then alter the x position based upon how they were shot. If from left, +=20, else -=20. Something like that. Is that what you're looking for?

All programming problems can be solved with Arrays!

BBS Signature

Sad

Petwoip

Reply To Post Reply & Quote

Posted at: 5/14/08 12:01 AM

Petwoip LIGHT LEVEL 04

Sign-Up: 01/18/05

Posts: 227

I don't think thats what I'm looking for. To clear some confusion, I'll write the code I have, so you can know what I'm attempting:

object.onEnterFrame = function(){
		for (i=0; i<360; i+=5) {
			X = this._x + (Math.cos(i*(Math.PI/180))*radius)+_x;
			Y = this._y + (Math.sin(i*(Math.PI/180))*radius)+_y;
			if (floor.hitTest(X, Y, true) or(all the objects inside myArray [i dont know how to do this])).hitTest(X,Y, true)){
			pointstop();
           		break;
			}
			
		}
		function pointstop(){
			totalhits+=1
			DupedMC = _root.attachMovie("MC", "MC"+i,i,{_x:location._x, _y:location._y})
			var myArray:Array = new Array(); 
			for(i=0; i< totalhits; i++){
			myArray[i] = _root["MC" + i] 
			}

		}

Now for explanation :
An object either hits the floor or a member or my Array (which consists of duplicated Movie Clip "MC"). When this hit occurs, pointstop function is called. The total # of hits increases by 1, and "MC" is duplicated at the location where the object hit the floor or one of the objects in the array. An array is created which holds, based on the number of hits there have been," i" number of duplicated "MC"'s. Basically I've tried to set up an array that comprises of all the duplicated MC's. I need to use these values in the array as objects that can be collided with, as written in the hitTest section. This may all make more sense when you view the link to my game:
Here

So I want each following ball to attach to any of the others that have fallen. That is why I need all the duplicated MC's to be together in an array and for the array to have some sort of hitTest ability. How do I accomplish all this?

.

BBS Signature

None

ElDebugger

Reply To Post Reply & Quote

Posted at: 5/14/08 01:06 AM

ElDebugger NEUTRAL LEVEL 01

Sign-Up: 04/11/08

Posts: 161

Boy, this is getting out of hand. I felt dizzy reading the entire post and responses...twice!

First, I would like to request that instead of asking "how to code actionscript to enter loops and create arrays and verify hitTests", I would like for you to instead rise one level above this thinking and say what you want your game to do. For example, explain "I have random black spheres falling from the sky. I have designed the game such that when a ball hits the floor, (bla-bla) happens. If a ball hits another ball instead of the floor, (bla-bla) happens. I think I can help you create code from this level.

Second, I feel that creating an array is something you should avoid unless you really have a reason to store a long list of things that have to be sorted in some fashion. Just because you say "I am going to create a LIST of things" you shouldn't immediately say "LIST = array". An easy way around your hitTest problem is to not create a hitTest that is connected to the array. It's too complicated! Just embed a simple hitTest inside each movieClip. When your main code duplicates it, the hitTest comes inside it. Easy!

Another thing...you used the term "indefinite" when describing the amount of elements you are planning on using inside your array. Are you really making a game where you create an infinite amount of objects--with the list just growing without ever ending? I used to think about firing bullets from my character's gun in terms of an array. I fire a bullet: the "bullet array" counts one more. But that means that by level 5, the array would have some ridiculous number, like "bullet #502". Realistically, I would only have 3 bullets on the screen at any given time, since bullets fly fast. None would last too long on the stage. So, instead of creating some complicated array that named a bullet every time I fired one, I simply created a varible that started at 0 and incremented to 5. If I fired a bullet, this variable would name the bullet, but also would reset to 0 when it reached the 5 maximum. No need to "track" the bullet in an array.

Does this help?

Got a broken PSP? Click here and have it fixed for cheap!

BBS Signature

None

LCurtis

Reply To Post Reply & Quote

Posted at: 5/14/08 01:52 AM

LCurtis NEUTRAL LEVEL 17

Sign-Up: 05/23/07

Posts: 1,075

At 5/14/08 01:06 AM, ElDebugger wrote: Boy, this is getting out of hand. I felt dizzy reading the entire post and responses...twice!

First, I would like to request that instead of asking "how to code actionscript to enter loops and create arrays and verify hitTests", I would like for you to instead rise one level above this thinking and say what you want your game to do.

If you look one post above yours you will find some irony.
And let me say I dont think people should write what they want their game to do. I think its better for them to learn about the elements behind a game. That way they can try and apply the concepts on their own rather than someone giving copy-pastable code (which is what happens when people ask directly).
I applaud this poster on his patience with learning. He has not once asked for (or demanded) any straight code. I gave him the concepts behind arrays and for loops and he is trying to learn them Note how he actually posted HIS OWN CODE that HE TRIED. Something most people dont do.

Second, I feel that creating an array is something you should avoid unless you really have a reason to store a long list of things that have to be sorted in some fashion. Just because you say "I am going to create a LIST of things" you shouldn't immediately say "LIST = array". An easy way around your hitTest problem is to not create a hitTest that is connected to the array. It's too complicated! Just embed a simple hitTest inside each movieClip. When your main code duplicates it, the hitTest comes inside it. Easy!

You have a site called fixmyactionscript.com. I hope no one uses it. While you are right, generally an array isnt the best way to implement a list but it is one of the easiest ways. And if you plan to help people with actionscript I hope you learn a few good practices first. Things like putting code directly into movieclips is a terrible way to program. GTFO with that crap.

Some more slander against arrays...

Does this help?

No. You should have been using removeMovieClip() once the bullets left the gameplay area and removed the "bullets" from teh array. Prevents you from having huge arrays like that :D

Finally, to get back to the hitTest question. No there wont be any simple way to just hitTest all of the objects in the array at once. You will need to use a for loob and check the object against each individual in the array. Then maybe set a variable to true if it hits. Im too tired now to edit your code or make any comments on it other than it doesnt look great but at least you are trying. Maybe another person can help tonight...

support the dream ...

BBS Signature

None

ElDebugger

Reply To Post Reply & Quote

Posted at: 5/14/08 02:17 AM

ElDebugger NEUTRAL LEVEL 01

Sign-Up: 04/11/08

Posts: 161

LCurtis, my post wasn't for you. It was for the original author of the post. If you have something to say about my suggestion, you can PM me directly and I will deal with you there.

I know that putting AS on a movieclip is a bad practice, but for a situation where AI is needed, I opt for embedding it into the object that uses the AI. In this case, a hitTest embedded inside each object sounds a lot easier to deal with than to setup an array that has to check each duplicated MC, despite whatever emotional attachment using arrays dictates to a person (LCurtis).

There is no "irony" in the statement above my original one. The author of the post did not specify the design of his game and how the parts interact, without mentioning code. A basic idea behind the workings of each game object helps as a good starting point to suggest an approach for coding the game. LCurtis thought that doing so implies that the author is simply asking for a reader to code said game idea to "copy and paste" (I never mentioned offering such code).

LCurtis is angry and in the mood for some red meat this evening. I am so delighted to hear it!

Got a broken PSP? Click here and have it fixed for cheap!

BBS Signature

Elated

LCurtis

Reply To Post Reply & Quote

Posted at: 5/14/08 02:35 AM

LCurtis NEUTRAL LEVEL 17

Sign-Up: 05/23/07

Posts: 1,075

At 5/14/08 02:17 AM, ElDebugger wrote: LCurtis, my post wasn't for you. It was for the original author of the post. If you have something to say about my suggestion, you can PM me directly and I will deal with you there.

Hello and welcome to a public forum :D. Your post was clearly at the OP. However, a forum is used for discussion. You made a post and I discussed. I wont bother PMing you about the topic since that would defeat the purpose of the topic.

I know that putting AS on a movieclip is a bad practice

And yet you suggest it anyway. We are here to help people and you dont do them any justice steering them down the wrong paths.

but for a situation where AI is needed, I opt for embedding it into the object that uses the AI.

I agree, an object orient solution would be best here. However you instructed him to do it in the wrong way. And more to the point, he asked about for loops and arrays.

sounds a lot easier to deal with than to setup an array

Welcome also to programming. Things often sound easier to start with but end up more trouble than they are worth.

that has to check each duplicated MC, despite whatever emotional attachment using arrays dictates to a person (LCurtis)

Passive aggressive attacks help no one D:


There is no "irony" in the statement above my original one. The author of the post did not specify the design of his game and how the parts interact, without mentioning code. A basic idea behind the workings of each game object helps as a good starting point to suggest an approach for coding the game. LCurtis thought that doing so implies that the author is simply asking for a reader to code said game idea to "copy and paste" (I never mentioned offering such code).

Please dont put words in my mouth. I said usually when people give a description of how they want their code to perform someone (with good intentions) jumps in and posts the required code (more or less) because they are eager to show their skills (usually people new to AS). I didnt mean to insinuate that you would do this but someone probably would.

LCurtis is angry and in the mood for some red meat this evening. I am so delighted to hear it!

I'm not angry. I would waste emotions on an online forum :D

Personally, I think the OP should either create a system for dealing with each object and running tests on it or design a class that specifically handles its own movement. However I prefer the former since each movieclip will need a reference to the other movieclips that it wishes to hitTest against. This is were the single array and double for loops shine.

Every frame you run a doubled up for loop like I posted above that checks for collision between each MC.

Since ElDebugger is feeling randy tonight, maybe he would like to post psuedo code of how he would implement his system?

PS you are allowed to respond to me directly instead of in the third person!

support the dream ...

BBS Signature

All times are Eastern Daylight Time (GMT -4) | Current Time: 03:18 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!