Connecting Tech Pros Worldwide Forums | Help | Site Map

Character Coin Collection Confusion

Member
 
Join Date: Feb 2009
Location: Originally York (UK) now Carmartheshire (South Wales).
Posts: 52
#1: Mar 5 '09
Hi I'm using AS2 to produce a simple platform game. So far so good I would like the character to collect coins or whatever. so what I have done is create a coin movie clip, I've run a hittest on the coin and my character and when this = true set the coin to _visible = false, move it way off screen and add 10 point to the _global.score. all is working fine but I haven't a clue about having more than one instance of the coin, or more precisely I do have a clue but I don't want to have to name all the instances differently and have reams of if statements (one for each instance). I'm sure that there is an easier way and I guess that I just need some direction.

I am new to AS2 so please go easy on me.

Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,938
#2: Mar 5 '09

re: Character Coin Collection Confusion


The duplicateMovieClip() method may help you here.
Member
 
Join Date: Feb 2009
Location: Originally York (UK) now Carmartheshire (South Wales).
Posts: 52
#3: Mar 6 '09

re: Character Coin Collection Confusion


am I right in thinking that the duplicate method requires you to rename each instance?? so if I wanted to refer to a particular instance I would need to still have a separate set of code for each hit test??
Member
 
Join Date: Feb 2009
Location: Originally York (UK) now Carmartheshire (South Wales).
Posts: 52
#4: Mar 6 '09

re: Character Coin Collection Confusion


I spoke to a some one I know about this, he suggested an array, which I can kind of see his point but I don't really know how to implement it, and he has not used flash before.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,938
#5: Mar 6 '09

re: Character Coin Collection Confusion


My idea is that, you have your movieclip (with hitTest() in an event handler for it [1]) and then you duplicate that movie clip. You don't have to reference the instance name to work with it (just use this in your hitTest().

Does that make sense?

[1]
Expand|Select|Wrap|Line Numbers
  1. onClipEvent(mouseMove)
  2. {
  3.     if( this.hitTest( _root._xmouse, _root._ymouse, true ) )
  4.     {
  5.         this._visible = false;
  6.     }
  7. }
  8.  
Member
 
Join Date: Feb 2009
Location: Originally York (UK) now Carmartheshire (South Wales).
Posts: 52
#6: Mar 6 '09

re: Character Coin Collection Confusion


Yep, the light dawned this afternoon. I was putting the code into the character and not the object. so from my perspective I had to reference a different coin each time instead of as you suggest, get all the coins to ref the same character. A school boy error, thanks for your time it has helped me get there.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,938
#7: Mar 6 '09

re: Character Coin Collection Confusion


Quote:

Originally Posted by Chinde View Post

Yep, the light dawned this afternoon. I was putting the code into the character and not the object. so from my perspective I had to reference a different coin each time instead of as you suggest, get all the coins to ref the same character. A school boy error, thanks for your time it has helped me get there.

No problem, friend. We all (in the words of JD, from Scrubs) "have a crystalizing moment when everything becomes clear." Unfortunately, we have the opposite a lot more often!

- Mark.
Newbie
 
Join Date: May 2009
Location: U.S.A.
Posts: 5
#8: May 13 '09

re: Character Coin Collection Confusion


Quote:

Originally Posted by Markus View Post

My idea is that, you have your movieclip (with hitTest() in an event handler for it [1]) and then you duplicate that movie clip. You don't have to reference the instance name to work with it (just use this in your hitTest().

Does that make sense?

[1]

Expand|Select|Wrap|Line Numbers
  1. onClipEvent(mouseMove)
  2. {
  3.     if( this.hitTest( _root._xmouse, _root._ymouse, true ) )
  4.     {
  5.         this._visible = false;
  6.     }
  7. }
  8.  

I tried using this code to solve a similar gem problem. using the [this._visible = false;], I put it into my hero. When he hits the gem however, it removes my hero. I am using the arrow keys to move my hero instead of a mouse. My current code.

Expand|Select|Wrap|Line Numbers
  1. if (_root.levels.GemTotem.hitTest(_x, _y, true)) {
  2.          this._visible = false;
  3.     }
  4.  
GemTotem is the Gem, Levels is the symbol the gem is under. Along with the hero and exit. Any ideas?
Member
 
Join Date: Feb 2009
Location: Originally York (UK) now Carmartheshire (South Wales).
Posts: 52
#9: May 13 '09

re: Character Coin Collection Confusion


It sounds exactly like the problem I was having. I moved the code for the hit test from my hero and moved it onto the Coin/Gem type thing. When you use
Expand|Select|Wrap|Line Numbers
  1. this._dosomething
it is referring to the person/place where the code is so when you say
Expand|Select|Wrap|Line Numbers
  1. this._visible = false;
it would make the hero (where you put the code) invisible. Just move the code to the Gem and change all the references there and see what happens.

Another problem which I found is that just setting the visibility to false will still leave the item there so, if you are running a scoring system the hero could just stand on the spot and rack up a massive score. In the end I move the object off screen too and this solved that problem.
Member
 
Join Date: Feb 2009
Location: Originally York (UK) now Carmartheshire (South Wales).
Posts: 52
#10: May 13 '09

re: Character Coin Collection Confusion


Quote:
In the end I move the object off screen too and this solved that problem.
Just realised I might be scolded of advising to do this for memory guzzling reasons.
Newbie
 
Join Date: May 2009
Location: U.S.A.
Posts: 5
#11: May 13 '09

re: Character Coin Collection Confusion


Quote:

Originally Posted by Chinde View Post

It sounds exactly like the problem I was having. I moved the code for the hit test from my hero and moved it onto the Coin/Gem type thing. When you use

Expand|Select|Wrap|Line Numbers
  1. this._dosomething
it is referring to the person/place where the code is so when you say
Expand|Select|Wrap|Line Numbers
  1. this._visible = false;
it would make the hero (where you put the code) invisible. Just move the code to the Gem and change all the references there and see what happens.

Another problem which I found is that just setting the visibility to false will still leave the item there so, if you are running a scoring system the hero could just stand on the spot and rack up a massive score. In the end I move the object off screen too and this solved that problem.

I tried as you said, but for some reason it won't work now. I'm still going to experiment with some things, but at the moment switching it didn't help.

Changed and referenced. However, I am new to Flash. Only been learning at school for about 6 - 7 weeks now. I might be forgetting something.

Current (not working) code for my gem.

Expand|Select|Wrap|Line Numbers
  1. onClipEvent (enterFrame) {
  2.     if (_root.levels.hero.hitTest(_x, _y, true)) {
  3.          this._visible = false;
  4.     }
  5. }
  6.  
Member
 
Join Date: Feb 2009
Location: Originally York (UK) now Carmartheshire (South Wales).
Posts: 52
#12: May 14 '09

re: Character Coin Collection Confusion


It's been a while since I looked at any AS2 Code (Been up to my neck in VBA) so I am maybe a little rusty and I wasn't 100% even when I was doing a lot of AS2 code.

I would try

Expand|Select|Wrap|Line Numbers
  1. if (this.hitTest(_root.levels.Hero._x, _root.levels.Hero._y, true)) {
  2.          this._visible = false;
  3.     }
I guess that the only difference is that I'm being clear on whose _x and _y it should be looking at and I'm switching the hittest to the Gem
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,938
#13: May 14 '09

re: Character Coin Collection Confusion


How strange that you're originally from York. I live in York :)
Member
 
Join Date: Feb 2009
Location: Originally York (UK) now Carmartheshire (South Wales).
Posts: 52
#14: May 14 '09

re: Character Coin Collection Confusion


I noticed that and I wondered what the with Wolves bit was about, if you mean you live in or near Tanghall I could understand.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,938
#15: May 14 '09

re: Character Coin Collection Confusion


Quote:

Originally Posted by Chinde View Post

I noticed that and I wondered what the with Wolves bit was about, if you mean you live in or near Tanghall I could understand.

I cannot remember whether I was calling my family wolves, or...

No not Tanghall, thank god! Strensall, on the outer area of York, near Haxby.
Member
 
Join Date: Feb 2009
Location: Originally York (UK) now Carmartheshire (South Wales).
Posts: 52
#16: May 14 '09

re: Character Coin Collection Confusion


It's a small world wide web.

Does the code I placed look Ok??
Newbie
 
Join Date: May 2009
Location: U.S.A.
Posts: 5
#17: May 14 '09

re: Character Coin Collection Confusion


Quote:

Originally Posted by Chinde View Post

It's been a while since I looked at any AS2 Code (Been up to my neck in VBA) so I am maybe a little rusty and I wasn't 100% even when I was doing a lot of AS2 code.

I would try

Expand|Select|Wrap|Line Numbers
  1. if (this.hitTest(_root.levels.Hero._x, _root.levels.Hero._y, true)) {
  2.          this._visible = false;
  3.     }
I guess that the only difference is that I'm being clear on whose _x and _y it should be looking at and I'm switching the hittest to the Gem

Yeah, I has a friend of mine come help me fix it. We attached the code to the hero stating.

Expand|Select|Wrap|Line Numbers
  1. if (_root.levels.GemTotem.hitTest(this._x, this._y, true)) {
  2.         _root.levels.GemTotem._visible = false;
  3.     }
  4.  
Had to add the workspace and declare this. Also had to link it to AS3.
Member
 
Join Date: Feb 2009
Location: Originally York (UK) now Carmartheshire (South Wales).
Posts: 52
#18: May 15 '09

re: Character Coin Collection Confusion


I might be wrong but if you intend to have many gems on one level you will have to have different code for each gem (as they should all have different instance names, otherwise if they all had the same name they would all vanish at the same time.). But I haven't done much AS3 yet so who am I to talk.
Newbie
 
Join Date: May 2009
Location: U.S.A.
Posts: 5
#19: May 15 '09

re: Character Coin Collection Confusion


Quote:

Originally Posted by Chinde View Post

I might be wrong but if you intend to have many gems on one level you will have to have different code for each gem (as they should all have different instance names, otherwise if they all had the same name they would all vanish at the same time.). But I haven't done much AS3 yet so who am I to talk.

Nah, Just one gem per level for now. I'll get into more advanced coding like that over the summer. :]
Reply