Be a Supporter!

AS: _name (and more)

  • 4,455 Views
  • 15 Replies
New Topic Respond to this Topic
Blaze
Blaze
  • Member since: Aug. 4, 2005
  • Offline.
Forum Stats
Member
Level 22
Programmer
AS: _name (and more) Jan. 7th, 2006 @ 07:28 PM Reply

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:.

reelbigcheese
reelbigcheese
  • Member since: Aug. 12, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to AS: _name (and more) Jan. 7th, 2006 @ 07:30 PM Reply

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

JPI
JPI
  • Member since: May. 9, 2005
  • Offline.
Forum Stats
Member
Level 21
Blank Slate
Response to AS: _name (and more) Jan. 7th, 2006 @ 07:33 PM Reply

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
Neashir
Neashir
  • Member since: Aug. 8, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to AS: _name (and more) Jan. 7th, 2006 @ 07:34 PM Reply

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

Blaze
Blaze
  • Member since: Aug. 4, 2005
  • Offline.
Forum Stats
Member
Level 22
Programmer
Response to AS: _name (and more) Jan. 7th, 2006 @ 07:38 PM Reply

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!

Rantzien
Rantzien
  • Member since: Jan. 27, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: _name (and more) Jan. 7th, 2006 @ 07:39 PM Reply

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
Blaze
Blaze
  • Member since: Aug. 4, 2005
  • Offline.
Forum Stats
Member
Level 22
Programmer
Response to AS: _name (and more) Jan. 7th, 2006 @ 07:41 PM Reply

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.

Rantzien
Rantzien
  • Member since: Jan. 27, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: _name (and more) Jan. 7th, 2006 @ 07:44 PM Reply

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

Not owned really =)


BBS Signature
authorblues
authorblues
  • Member since: Jun. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: _name (and more) Jan. 7th, 2006 @ 07:51 PM Reply

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
Rantzien
Rantzien
  • Member since: Jan. 27, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: _name (and more) Jan. 7th, 2006 @ 08:02 PM Reply

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
authorblues
authorblues
  • Member since: Jun. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: _name (and more) Jan. 7th, 2006 @ 08:06 PM Reply

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
Blaze
Blaze
  • Member since: Aug. 4, 2005
  • Offline.
Forum Stats
Member
Level 22
Programmer
Response to AS: _name (and more) Jan. 7th, 2006 @ 08:09 PM Reply

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. ;)

Rantzien
Rantzien
  • Member since: Jan. 27, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: _name (and more) Jan. 7th, 2006 @ 08:13 PM Reply

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
Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to AS: _name (and more) Jan. 7th, 2006 @ 08:35 PM Reply

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

authorblues
authorblues
  • Member since: Jun. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: _name (and more) Jan. 7th, 2006 @ 10:03 PM Reply

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
Wampo
Wampo
  • Member since: Dec. 14, 2006
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to AS: _name (and more) May. 27th, 2008 @ 08:32 AM Reply

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