A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: [HELP] Score code in the main layer?

  1. #1
    I'm not Crazy :D
    Join Date
    Nov 2015
    Location
    Brazil [<o>]
    Posts
    86

    Question [HELP] Score code in the main layer?

    Hey everbody

    I try make a code to work with all coins existing in the game, but not work correctly. I inserted the code in the first layer of the timeline. The code is this:

    Code:
    coin.onEnterFrame = function()
    {
    	if (_root.player_mc.hitTest(this))
    	{
    		score += 1;
    		unloadMovie(this);
    	}
    }

    The problem is this: when I picked up a coin, works only time I try to pick up the second currency, the code does not work. How can I fix this? Thank you anyway

  2. #2
    I'm not Crazy :D
    Join Date
    Nov 2015
    Location
    Brazil [<o>]
    Posts
    86
    Sorry for the spam.

    See the problem here (do not forget to click on the game to work): http://www.fastswf.com/xbianPQ

  3. #3
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    the code is correct, but you have to do it for *every* coin. just pasting the code above will only work for one of them. you could put this into the coin mc, doing this.onEnterFrame = ..., instead.
    who is this? a word of friendly advice: FFS stop using AS2

  4. #4
    I'm not Crazy :D
    Join Date
    Nov 2015
    Location
    Brazil [<o>]
    Posts
    86
    Hey man.. I've done it.. but somehow.. do not "corrupt" game? For example, start getting lag in the game?

  5. #5
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    yes, of course, because hit test is expensive operation. now you're looking into optimizing this, for example running the test only if the coin is visible on the screen, or within 100 pixels from the hero
    who is this? a word of friendly advice: FFS stop using AS2

  6. #6
    I'm not Crazy :D
    Join Date
    Nov 2015
    Location
    Brazil [<o>]
    Posts
    86
    Nice! Any idea about how to make this? Example code or a link? c.c

  7. #7
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    assuming coins are also in the _root, try changing
    PHP Code:
    if (_root.player_mc.hitTest(this)) 
    to
    PHP Code:
    if ((Math.abs(_root.player_mc._x this._x) < 100) && _root.player_mc.hitTest(this)) 
    who is this? a word of friendly advice: FFS stop using AS2

  8. #8
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    oh, another idea is that as2 might be upset by the number of onEnterFrame handlers this creates. so, if the above hitTest guess is not correct, try creating onEnterFrame on player instead and loop over coins there.
    who is this? a word of friendly advice: FFS stop using AS2

  9. #9
    I'm not Crazy :D
    Join Date
    Nov 2015
    Location
    Brazil [<o>]
    Posts
    86
    Nice code man, if I want that this code also works with y, just replace the x or need to make any more changes?

    The other idea is awesome, I will test it.

  10. #10
    I'm not Crazy :D
    Join Date
    Nov 2015
    Location
    Brazil [<o>]
    Posts
    86
    Hey... sorry for the resurrected the topic, but I was still having trouble about it. I've searched various forms of loop the coins but found nothing. Anyone has any idea?

  11. #11
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    who is this? a word of friendly advice: FFS stop using AS2

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center