00:00
00:00

AS: _name (and more)

5,176 Views | 15 Replies
New Topic Respond to this Topic

AS: _name (and more) 2006-01-07 19:28:07


T3h AS: Main

___________________________________
Well, nothing but another crappy AS thread by me. I dont see any other thread that would explain this, and im not very good at explaining. But ive gotten the hang of this stuff; so here goes:
___________________________________
What you will learn:
Very basic stuff, using the _name property (yay, finally got it right) and indexOf.
___________________________________

Ok, here goes

What can this be useful for?

Well, ive used it for hitTesting duplicated MCs. There is another way, explained in a tutorial by another NG Member, or you can always use Arrays. But in my own opinion, arrays are hard to understand - sure, they can be very bloody useful once you get the hang of them though.

Okay, into the tutorial.

The _name property, basically checks the name of an object. Say, it could be used combining IF statements.

if(this._name=="name"){
//do stuff
}

Thats a very basic form of it. I dont think theres much i can explain about this topic though.

Moving on.

the indexOf is often used with the _name property. It checks if the name of something starts with what you set. Ive used it quite a lot of times, again, for hitTesting duped MCs. Like this:

First off, i take it you know how to Duplicate MovieClips. Say, that your movieclip is duplicated like this:

this.duplicateMovieClip("name"+i,1337+i);
//im not going to go into further detail on for loops,for more check: Here

Then, you want to hitTest the duped MC. Id go for this:

put this code on a bullet or something to hit.

onClipEvent(enterFrame){
for(i in _root){
if([i]._name.indexOf("thing")!=-1){
if(_root[i].hitTest(this)){
//do stuff
}
}
}

thats pretty much about it.

Hehe, my second tutorial. Not very well explained, maybe even has been made before. But i just wanted to do this.

Hope it helped.

.:Blaze:.

Response to AS: _name (and more) 2006-01-07 19:30:27


nice, i already knew all this stuff but some people will find it useful XD

Response to AS: _name (and more) 2006-01-07 19:33:31


A very specific, but very useful tutorial Blaze :D
Be sure to try to add it to AS: Main

And because all the cool kids are doing this:

.:Blaze:.

<::J::> <::A::> <::M::> <::E::> <::S::>

P

SIG YOINK!

BBS Signature

Response to AS: _name (and more) 2006-01-07 19:34:24


Now I get the idea of indexOf. Helpful to me! Good job, and now that makes 2 AS: Topics by you :D

Response to AS: _name (and more) 2006-01-07 19:38:32


At 1/7/06 07:30 PM, -reelbigcheese- wrote: nice, i already knew all this stuff but some people will find it useful XD

Thanks.

At 1/7/06 07:33 PM, James_Prankard_Inc wrote: A very specific, but very useful tutorial Blaze :D
Be sure to try to add it to AS: Main

Yep, thanks. I already posted it on AS: Main.


And because all the cool kids are doing this:
<::J::> <::A::> <::M::> <::E::> <::S::>

=O im not worthy. XD im not cool.

At 1/7/06 07:34 PM, Neashir wrote: Now I get the idea of indexOf. Helpful to me! Good job, and now that makes 2 AS: Topics by you :D

;) Great thing you finally got it. And yes, two threads! go me!

Response to AS: _name (and more) 2006-01-07 19:39:15


At 1/7/06 07:28 PM, Darkfire_Blaze wrote: onClipEvent(enterFrame){
for(i in _root){
if([i]._name.indexOf("thing")!=-1){
if(_root[i].hitTest(this)){
//do stuff
}
}
}

A much more efficient way is to push them into an array at the moment of duplication, or even give them an onEnterFrame handler right there =)

Example to show what I mean:

lolomg = this.duplicateMovieClip ("omfg" + ++curD, curD);
myArray.push (lolomg);

or:

lolomg = this.duplicateMovieClip ("omfg" + ++curD, curD);
lolomg.onEnterFrame = function () {
// stuff
};


BBS Signature

Response to AS: _name (and more) 2006-01-07 19:41:14


At 1/7/06 07:39 PM, Rantzien wrote: code

omfg. Owned once again. =O

I pointed that out, you can also use arrays - it might be even better. But i dont know, i just hate arrays for some reason.

i forgot to mention, authorblues was the one that first taught me this. And neashir gave me the idea for this thread.

Response to AS: _name (and more) 2006-01-07 19:44:02


At 1/7/06 07:41 PM, Darkfire_Blaze wrote: omfg. Owned once again. =O

Not owned really =)


BBS Signature

Response to AS: _name (and more) 2006-01-07 19:51:30


At 1/7/06 07:39 PM, Rantzien wrote: A much more efficient way is to push them into an array at the moment of duplication, or even give them an onEnterFrame handler right there =)

the benefits to doing this it this way are few, but here are some i can come up with. it easily allows you to set different possible ways to handle hittests against different objects, and it allows the object being hit to run the hittest code instead of the bullet, making this hittest in the form of one for loop, as opposed to multiple objects each with seperate onEnterFrame handlers. i cant image that could be very cpu-conservative.

the only problem i can think of with the array is that youre using an extra slot to store the same information that can be retrieved in this way. of course, running thru an array of pre-decided objects is probably a bit more proper than checking every object for its qualities, and making this decision every frame.


BBS Signature

Response to AS: _name (and more) 2006-01-07 20:02:34


At 1/7/06 07:51 PM, authorblues wrote:
At 1/7/06 07:39 PM, Rantzien wrote: A much more efficient way is to push them into an array at the moment of duplication, or even give them an onEnterFrame handler right there =)
the benefits to doing this it this way

Hmm. By 'this way', do you mean my way or the highway, uh, I mean blaze's way?


BBS Signature

Response to AS: _name (and more) 2006-01-07 20:06:13


At 1/7/06 08:02 PM, Rantzien wrote: Hmm. By 'this way', do you mean my way or the highway, uh, I mean blaze's way?

awww, cute. since my explanation supports my/blaze's way, i think we are talking about the _name.indexOf(str); method... and your way is the pits.


BBS Signature

Response to AS: _name (and more) 2006-01-07 20:09:14


At 1/7/06 08:06 PM, authorblues wrote: awww, cute. since my explanation supports my/blaze's way, i think we are talking about the _name.indexOf(str); method... and your way is the pits.

hehe. Awesome, thanks a lot for pointing all that stuff up. ;)

Response to AS: _name (and more) 2006-01-07 20:13:21


At 1/7/06 08:06 PM, authorblues wrote: awww, cute. since my explanation supports my/blaze's way, i think we are talking about the _name.indexOf(str); method... and your way is the pits.

Grarr, wanna duel?

it easily allows you to set different possible ways to handle hittests against different objects, and it allows the object being hit to run the hittest code instead of the bullet, making this hittest in the form of one for loop, as opposed to multiple objects each with seperate onEnterFrame handlers. i cant image that could be very cpu-conservative.

Now several onEnterFrame handlers are probably not "cpu-conservative", as you call it in your foolish manner. But the indexOf method sure isn't either. It would be better to store the object in an array, or possibly to use the more classical method:

onClipEvent (enterFrame) {
for (i = 0; i < 55000432; i++) {
trace (hitTest (_parent["bullet" + i]);
}
}


BBS Signature

Response to AS: _name (and more) 2006-01-07 20:35:39


or instead of arrays, store all the bullets in a MC, then use a for.. in of that MC!

Response to AS: _name (and more) 2006-01-07 22:03:15


At 1/7/06 08:35 PM, Glaiel_Gamer wrote: or instead of arrays, store all the bullets in a MC, then use a for.. in of that MC!

that requires a shapeflag, which is ugly, and only allows you to test point-vs-object. what if youre storing all of your enemies in an MC as well?


BBS Signature

Response to AS: _name (and more) 2008-05-27 08:32:47


I didn't get it with the indexOf thing.. could you explain it.. or maybe just make an easier code to understand it?! Because i want an duped bullet hits a duped enemy...


Did you know Wampo is a river in south africa?

Unbelieveble :O