Jump to content

How to add coins that have specific positions via groups?


potatopoo
 Share

Recommended Posts

Could you please slap me a piece of code thats repetitive instead of saying 2 words, like i know what im doing, tnx :P I know that another alternative is object layer from tiled.. but im not sure exactly how one would animate the sprites placed. I'm looking for classic coin behaviour.. lots of them, using 1 animation (an one on pickup), making 1 sound, dissappearing and giving +1 to some counter. Tnx

Link to comment
Share on other sites

Tnx man, helped a lot. I also found a way to put the coins in specific positions. Dont have a counter yet to see if it all works smooth, but for now i can pick up the coins. This is what i did:create: function () {//creating variablesthis.coins;this.coinCG;this.pickupCs;//creating coin groupthis.coins = this.add.group();this.coins.enableBody = true;this.coins.physicsBodyType = Phaser.Physics.ARCADE;this.createCoins();//creating pickup animation groupthis.pickupCs = this.add.group();this.pickupCs.createMultiple(3, 'coin2');this.pickupCs.forEach(this.lePickup, this); },update: function () {//checking for overlap between coin and player, initiating the coinFunctionthis.physics.arcade.overlap(this.player, this.coins, this.coinFunction, null, this);},//creates 3 coins that are immovable in specific locationscreateCoins : function() {        for (var i = 1; i <= 3; i++) {                        if (i == 1) {                var coinCG = this.coins.create(140,450,'coinAnimation');                coinCG.animations.add('blink',[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],10,true);                coinCG.play('blink');                coinCG.body.moves = false;            }            else if (i == 2) {                var coinCG = this.coins.create(160,450,'coinAnimation');                coinCG.animations.add('blink',[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],10,true);                coinCG.play('blink');                coinCG.body.moves = false;            }            else if (i == 3) {                var coinCG = this.coins.create(180,450,'coinAnimation');                coinCG.animations.add('blink',[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],10,true);                coinCG.play('blink');                coinCG.body.moves = false;            }         };              },//loads the coin animation for each ilePickup : function(coin) {        coin.animations.add('coin2');  },//plays the pickup animation on overlap and then does somethingcoinFunction : function(player, coinCG) {        coinCG.kill();        var pickupanimation = this.pickupRs.getFirstExists(false);        pickupanimation.reset(coinCG.body.x, coinCG.body.y);        pickupanimation.play('coin2',30,false,true); },

I dont need the coins to reload after they were destroyed. No idea what this .reset part does, but hopefully this piece of code can help someone in the future.

And please, if someone has auggestions on how to make my code better, please tell me, im a total noob.

Link to comment
Share on other sites

reset is for garbage collection of your coins, think about it this way, Say you have a side scrolling platformer like mario, and there are a hundred coins in a single level. And you have to create a hundred coins, but that's inefficient, because the screen actually shows 10 coins at most at a time, so you have to reuse the existing coins. You will have a coin pool of say only 10 coins, and when you need to display a coin you take a coin from the pool, and when a coin goes out of screen you put the coin back to pool, so this way you can display 100 coins with just 10 coins in the pool, reusing coins. So `Sprite.reset` is resetting a coin that is outside of screen, and making it available for reuse. This will give you an idea, here are some tutorials that explain all of this better: Study the side scroller 4 part series tutorial here at the bottom: http://www.pixijs.com/resources/, also an excellent tutorial explaining everything about Phaser game development here http://blog.lessmilk.com/how-to-make-flappy-bird-in-html5-1/

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...