Character Coin Collection Confusion | Member | | Join Date: Feb 2009 Location: Originally York (UK) now Carmartheshire (South Wales).
Posts: 52
| | |
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.
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,938
| | | 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
| | | 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
| | | 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.
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,938
| | | 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] -
onClipEvent(mouseMove)
-
{
-
if( this.hitTest( _root._xmouse, _root._ymouse, true ) )
-
{
-
this._visible = false;
-
}
-
}
-
| | Member | | Join Date: Feb 2009 Location: Originally York (UK) now Carmartheshire (South Wales).
Posts: 52
| | | 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.
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,938
| | | re: Character Coin Collection Confusion Quote:
Originally Posted by Chinde 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
| | | re: Character Coin Collection Confusion Quote:
Originally Posted by Markus 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] -
onClipEvent(mouseMove)
-
{
-
if( this.hitTest( _root._xmouse, _root._ymouse, true ) )
-
{
-
this._visible = false;
-
}
-
}
-
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. -
if (_root.levels.GemTotem.hitTest(_x, _y, true)) {
-
this._visible = false;
-
}
-
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
| | | 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
it is referring to the person/place where the code is so when you say
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
| | | 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
| | | re: Character Coin Collection Confusion Quote:
Originally Posted by Chinde 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
it is referring to the person/place where the code is so when you say
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. -
onClipEvent (enterFrame) {
-
if (_root.levels.hero.hitTest(_x, _y, true)) {
-
this._visible = false;
-
}
-
}
-
| | Member | | Join Date: Feb 2009 Location: Originally York (UK) now Carmartheshire (South Wales).
Posts: 52
| | | 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 - if (this.hitTest(_root.levels.Hero._x, _root.levels.Hero._y, true)) {
-
this._visible = false;
-
}
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
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,938
| | | 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
| | | 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.
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,938
| | | re: Character Coin Collection Confusion Quote:
Originally Posted by Chinde 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
| | | 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
| | | re: Character Coin Collection Confusion Quote:
Originally Posted by Chinde 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 - if (this.hitTest(_root.levels.Hero._x, _root.levels.Hero._y, true)) {
-
this._visible = false;
-
}
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. -
if (_root.levels.GemTotem.hitTest(this._x, this._y, true)) {
-
_root.levels.GemTotem._visible = false;
-
}
-
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
| | | 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
| | | re: Character Coin Collection Confusion Quote:
Originally Posted by Chinde 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. :]
|  | Similar Flash / Actionscript bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,419 network members.
|