How do I fill an array with sprites and manipulate them

0 favourites
  • 15 posts
From the Asset Store
An educational game for Fill in the Blanks. An easy to use template for developers to build larger games
  • Hey, guys!

    I would need help with my inventory. I´ve Objects (Sprites) i want to store in an array. This Objects should be disabled, until you find them durring the game. The main problem is, that i dont know how to fill an array with sprites, and give them an identifier if theyre found or not. After that they should appear in the inventory (maybe with the opacity event).

    Saw many Tutorials, but iam not sure how to set it up currently on my own.

    Thanks a lot!

  • Hmmm another question: Ive created an Array which is 24 on Width and 1 on Height. I filled the X (Width) Slots with Instance names of the Objects i want to compare with the Height value (The Height Values should be "false" on each X Slot at the beginning). Durring the game i want to set those Y Values to ture, if the corresponding Spirtes are found by the player.

    Then the Objects should be enabled on my inventory.

    Would that be possible?

  • You need to create some sort of data structure, instead of trying to "store" sprites.

    What matters is the logical object the sprite represents.

    You can then, instead of using several sprites, use only a single Sprite with multiple animation frames (or animations).

    You should then have a multiple dimensions array which X will represent the "index" for each object, and the values on Y will represents parameters/properties of the object, for example, the animation name to use in the Sprite object type to display the logical object.

    One of the Y value should be either 0 or 1, 0 means the object hasn't been found, 1 it has.

    When initialising your array, you should set the maximum Y (Height) value possible right away.

    Even if you are not filling it right now (the default value of "empty" cells is 0 so it still contains a value).

    Resizing the array "on the fly" in Y risks of losing existing values and can actually be a logical nightmare to implement.

    If that did not help, consider posting your capx and explaining clearly what you are attempting to do, what you have done so far, and what you experience and how it differs from what you would expect.

  • Okay. Your inventory should just show the situation. The situation is baked in the Array. So, the inventory shows the array. Thats all.

    To couple something to an array, you need common ground. The most easy way is to give the cells in the inventory a number (instance variable) and that same number on the X-as (y=0) in the array.

    Now you can loop trough the (for each element) Array (with array.curX) and pick the cell with number = curX, to find the cell that goes with the array.

    But you can also loop trough the cells (for each) and pick the corresponding element on the X-as = cell number, to find that element in the array that goes with the cell.

    Maybe it makes no sense to you now, but i got a capx to back it up.

    On the Y-axis you store whatever you like. Just make a comment in your events what each Y is supposed to be.

    In this capx y1= animationframe and y2=amount of found items.

    https://www.dropbox.com/s/gdj8qoo8532jr ... .capx?dl=0

    I also wanted to show you how easy it is to use INSTANCES of the same object, that are differend by animationframe (or just by animation) to represent the items that can be found.

    As you see, it is really compact in events, yet this capx does a lot of things.

    Dont understand something, just ask.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 99Instances2Go Kyatric Thanks both of you for your advices! Your example file is awesome 99Instances2Go! I was stucking when i tried to set it up on my own, so i think i will take your example as "state of the art" to implement this events. Thank you very much!

    Currently i have only one question, before i will starting again to implement the inventory. Why do you guys use one sprite with differend animation frames for differend objects? I thought the animation panel is for animations, which depends on a specific sprite type/animation (like a walkcycle or something like that) not for basically differend sprite types

    Best regards!

  • A lot of reasons.

    It will reduce the amount of code, making it managable. Because you have just 1 object and its instances. And you get instance variables for free.

    Construct has all easy code in place to loop/pick/manage instances. Especaly loops are so much easyer. You can ofcours use families, but they lack IID support. And that can give you unexpected suprises.

    Because of the instance variables, and because of the easy looping, instances are easy to lock to an array.

    If you use instances (in stead of all differend objects) the only way to change its appereance is by changing its frame (or animation). So this is in fact is just a logical route that started with choosing to go with instances.

  • I'm not sure to understand your question actually.

    In Construct 2 we talk about object type and instances.

    Making the difference between both is important.

    We can have an object type sprite that we name "Item" and that contains different animation frames, or animations (in case you want to display the item as animated).

    Using instances of the "Item" object type allows to have code applied only to this object, but yet have this common code happening for all instances, and so in the end for all "Items" shown/used in the game's logic.

    It is simpler to use, doesn't require to duplicate events/actions changing the destination object type.

    It also allows to keep all assets in a single place, easier for access.

    Using animation frames number can also help having some logical connection with arrays indexes (they both start at 0, and so by checking what animation frame an instance has, you could be able to use this number to access the data for the item, stored in the array, quite easily).

    I hope this makes sense.

  • Kyatric 99Instances2Go and again thank you for your detailed explanations! Now i can understand the usage of this topic. I was asking, because i was importing all the sprites as own sprite object. But now iam using this kind of technic. It looks much more clear now. And if there are so many advantages...... lets go!

    So today i will try to make my inventar working. When its done i would may send you my capx. file

    Thank you very much for your help!

  • Hello again... so i was playing around with your capx file - it works like a charm. But at first: how can i stop the frames beeing animated? i could not find a checkmark or something to let them stay on their animation frame (In my project, in your project the sprites doesnt animate trough).

    If i have my inventory (its more a book, filled with sprites on their right place (but arent visible if you didnt found it)), and each sprite is placed on its final position. Would there be an easy way to synchronize the sprites with the corresponding spirtes i would place in the game?

    Better description:

    Like you know - the player has to follow footsteps on the ground. sometimes there are some symbols you can collect (new). If you collect this symbols - the corresponding one will appear in my inventory book. You cant remove them or use them. See them more as "achievements".

    Update: Ive created two Main Sprites with the exact same items on each animation frame. Now i would have to compare this two sprites, if i found the ingame sprite(or better symbol), and make it visible in my inventory book. The reason why i created two main sprites is that ive a trigger in the center of my screen which is colliding with this sprite objects. If youre pressing a button on the screen to open the book, only the layer gets invisible...so the colliders would still active, and the elements in the book gets removed. This causes many errors. So now i have to synchronize this two main sprites with each other. If i find for excample a symbol which is on the animationframe 5 slot, the symbol on the animationframe 5 in my inventory book should be visible.

    I know its maybe difficould to understand. But i hope you guys know how to do this!

    Thanks a lot - and if you need some more informations or a better description please write!

    Best regards!

  • Okay maybe there is a better solution for my inventary book (currently the layer will be set to invisible). Something like the Container behavior??

  • If that is wat you are creating, then i can not think at any reason to use an array.

    Instances and there instance variables are allready basic arrays.

    https://www.dropbox.com/s/630p82827qo3w ... .capx?dl=0

  • 99Instances2Go You´re crazy!

    I thought it would be the best way to handle this with arrays...maybe for storing the array values compact and manipulate them also compact. Funny i thought i have to use arrays for the footsteps and for the inventory book...but both thoughts where false. Thank you very much for your imput. It makes me better in understanding construct2!

    Very nice support! AWESOME!

  • Well, arrays are the way to go when you have to deal with lists AND when they un-complicate things.

    But to overtrow the easy and straightforward use of instances they are usaly not the best choice.

    Instances are allready a LIST of objects. If you give them instance varibales, you have a LIST of variables. Hence, that is in fact an array. Only, this array is locked to a particulair object.

  • Template in store if that helps - 1. Layout : summons random hero and pushes into arrays

    2. A inventory that lists all heroes + delete function

    https://www.scirra.com/store/royalty-fr ... ntory-2685

  • All

    Thank you very much for your great support!

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