Using the same action for multiple objects

0 favourites
  • 8 posts
From the Asset Store
With this template you will learn how to use the GooglePlay Games native plugin
  • Hello everyone, I'm new here and I need some help from you guys. I'm coding a quick demo from the old NES video game Super Mario Bros and despite everything looks kind of smooth and cool so far I cannot understand how to accomplish some feature. When Mario hits some blocks a few of them are supposed to spawn a coin which will add some points to the final score. Now I actually got it to work with the first block but if I keep using the same method with the others the coin will always be spawned in the first block only. What is the best approach to use the same animation of the first block over and over for the rest of the other blocks? I guess I should be using a function or a global variable or something rather than just copy/paste my assets.

    Here is my capx file dl.dropboxusercontent.com/u/1481984/supermariobros.capx

    Thank you all in advance!

    Luca

  • two things:

    • remove block2 and block3, use only block1
    • block2 and block3 dont' have any actions.
  • for your question: how to use the same action for different objects, for that you need to use Functions (first needs to be inserted to be able to use them)

  • Thank you Ghost. As of now I kind of managed to achieve the spawn coin thing although I'm still in trouble with the blank block that pops in after that. The whole process should be as follows: Mario hits one block which triggers the coin spawn feature which then turns the initial block into an unusable block. Now the problem is when Mario hits any block the coin does spawn correctly but all blocks turn into blank which is clearly inconvenient.

    You were previously mentioning to get the block's IID. How do I do that?

    Anyway here is my updated capx

    dl.dropboxusercontent.com/u/1481984/supermariobros.capx

    Thank you very much for your help!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Luca11,

    There are a couple of ways you could achieve the effect you want. First, there's a subtle but important point with respect to C2's event system which is causing your problem. On line 9 where you set the blankblock visible, you are selecting all blankblocks - you need to specify a particular instance for C2 to only change an individual's characteristics.

    So, you could use containers which would make this very easy (have a read of the manual and, after a bit of experimenting you'll never look back - wouldn't need any code change to your events, I think) or you could add a sub event to what you have already and pick the closest blankblock to the Block1 in the collision.

    There is another method you could follow. You could have the blocks as different frames of the same sprite. Check the frame of the collision block on collision - if it's 0 spawn a coin, if it's 1 do nothing, on collision change frame to 1...

  • Add Event > On Mario Colliding With Another Object

    Condition : BlockObject > Pick by unique ID

    Add Action > Spawn Coin At Co-ords

  • Here's a few notes from me. I'm a complete newb to Construct 2, so there are probably better ways to do everything than the way I may suggest! <img src="smileys/smiley2.gif" border="0" align="middle" />

    First, I noticed that your Background image was on the main layer. I moved it to the "Background" layer since I figured it belonged there. <img src="smileys/smiley2.gif" border="0" align="middle" />

    Secondly, I noticed that your block sprites are 32x32 pixels, yet in your level they're only 25x25? I don't know if that's intentional or not, but I thought I'd mention it just in case. Also with your level layout, it's usually easier to line things up if you use the snap to grid function. (Which is another reason I mentioned the size difference from 32x32 to 25x25.)

    There were also some hidden "landing_objects" placed under some of your coin blocks. I removed them since they were causing problems with the player hitting the coin boxes.

    Lastly, I changed the event where the player hits the box so that it changes to the "blankblock" without you having to place both in the scene overlapping each other.

    There's still some things that need to be worked out, but hopefully this gets you going in the direction you wanted to go! <img src="smileys/smiley1.gif" border="0" align="middle" />

    dl.dropboxusercontent.com/u/98323832/Construct2/supermariobros-altered.capx

  • Thank you guys, I really appreciate your effort to help me figure out how to do this thing! All of your suggestions seem to do the trick!

    Mcflygold

    Yes, the stage was quite messy when I shared my work with the community but now it's all cleared up in pretty much the same way you suggested. I didn't realize I didn't need to overlap the blocks so thanks for that as well! :)

    Now everything works correctly, I'll keep you guys posted with any improvement! :)

    See ya and have a nice sunday!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)