Best way of interacting with different instances?

0 favourites
  • 13 posts
From the Asset Store
Best car suspension with spring effect and very cool terrain generation.
  • I want to have more than one star in the level (1-5 max) that you have to collect, and the game needs to remember which stars have been picked when you reload the same level (for example by changing the color or opacity of the ones you already have collected).

    I guess I could do it by creating different sprites for each one, but I'd like to know if there is a cleaner way of doing it with different instances of the same sprite. Thanks.

  • I think it's easy, but i don't fully understand what do you want to make.

  • If you ever played Super Mario 64 for example, in that game you had to collect 6 different stars in a level, when you collected 1 star, the level finished. And afterwards you could go back to the same level to get the rest of the stars, and the ones you already had appeared greyed out to indicate that you already had those ones.

  • Basically you need a way to tell your stars appart (an instance variable, or its position maybe) and a way to store their status across layout (global variables or global array)

    For instance, set your star sprites to have an instance variable name 'ID'

    Manually set each ID to a unique number each time you create a new star.

    In events things could look like :

    Global variable pickedList = ""  // List of picked stars
    System: On start of Layout
    System: For "" from 0 to tokencount(pickedList,newline)-2
        Star: ID = tokenat(pickedList,loopindex,newline)
            -> Star: Destroy
    Player: On Collision with Star
        -> System: Set pickedList to picketList&Star.ID&newline
        -> Star: Destroy
  • Interesting, I wasn't aware newline could be used as the deliminator.

  • I often use "|" but newline looks better and is easier to read

  • Yeah, and it should display nicely in text as well.

    I do wonder if |, or a single character would be faster.

  • Thanks guys, I actually didn't know that you could assign different variables values to different instances of the same object, because some options carry across all instances and that got me confused.

    Back to the topic, I managed to do what I wanted using an array and an instance variable, assigning a different value to each instance (ID):

    <img src="" border="0" />

    Probably isn't the absolute best way, but at least I understand how this one works. The downside is that I have to copy and edit that part for each star on the level, but since there aren't many (1-3) I guess it's ok.

    Also, right now I cannot save the array information since there isn't a way to do it, webstorage is just for variables I think, so I'll have to wait to implement a save system that saves the stars count on each level (which are stored in the array). Suggestions are very welcome.

  • Basically you need a way to tell your stars appart (an instance variable, or its position maybe) and a way to store their status across layout (global variables or global array)

    I like to use BINARIES for that kind of storage, but this is not straight forward in C2.

    You can asign a bit for each star an flip them if collected.

  • I see, you might like my bit plugin I should release it

  • You don't need to do that for each star.

    Basically, when you have some repetitive task to do in programming, you have to tell yourself that there HAVE to be a way to avoid copy/pasting.

    Because after all... Good programmers are lazy programmers :D

    The way to avoid that is loops

    basically do a foreach on your stars and look at the corresponding value in the Array

    System: On start of layout
    System: foreach Star
    Array: Value at(Star.ID) = 1
       -> Star: Set opacity to 20
    FirefoxIcon(wtf?): On collision with Star
       -> Star: Set opacity to 20
       -> Array: Set value at(Star.ID) to 1

    And why do you need two dimensionnal Array? Value at (X,Y) you only need a Value at(X). If its value = 0 your star isn't picked, if it's 1 it is.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • That does work wonders indeed, thanks, it's a good trick to use the Star.ID to automatically select the position in the array.

    I wanted to use the Y axis to separate each game level in the array, 0 would be the first level, 1 would be the second're suggesting to just drop that and put every star next to each other in the X axis regardless of the level right?

    Yeah, firefox icon is just a temporary sprite <img src="smileys/smiley36.gif" border="0" align="middle" />

  • in that case use X for game level and Y for ID. That's not a bad idea because you then don't have to bother with star sharing the same ID in two different level.

    So Array: Value at (Level,Star.ID) is good.

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