reference the object I just created?

0 favourites
  • 15 posts
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • If I want to create a ball, then do stuff with it (e.g. move it around), that's easy. But If I want to create an instance of say a blue, black or red ball, then do the same stuff with which ever one I just created?

    E.g. inn bingo, the balls are different colours depending on the number just called.

    E.g. if the number is between 1 and 15, create a blue ball

    if the number is between 16 and 30, create a green ball.

    Then I want to do a whole lot of stuff with that ball. But I dont want to have to duplicate all that code for each ball colour, and I dont think you can pass balls (objects) in as parameters to a function.

    I actually solved the problem by making the balls different frames of a single sprite, but I'm interested if there is another way for cases where the sprite hack would not have worked?

    Many thanks for your thoughts.

  • You can pass a UID, and pick it again in the function. You have to be careful with creating and immediately picking. This has been documented many times on other threads, so I won't start that up again.

  • Er, if you cant create then pick, what is the recommended solution? Create, wait, then pick? That would seem tricky?

  • Ok, my solution did not work. I have tried the following:

    1) Spawning a copy of an existing (off layout) ball object, then using ball->set x etc. in actions in the same event block. This moves all balls, not the one just created.

    1) assigning a intance variable called "justCreated" and set it to 0. Use System->create object to create a new ball. In a sub event, select ball where justCreated = 0, and set it to 1. Do stuff to that ball. This doesnt work as it affects the newly created ball, AND the template/dummy ball which is only there to allow new balls to be created.

    Can anyone point me to a man page, thread, or capx which shows how to pick the object you just created/spawned?

    Thanks!

  • This does not seem to work for me. If I create a new ball object for example, then use ball.xx in the action of the same or a sub event, it always acts on all balls, not the one I just created.   As I don't know how to pick the event I just created, I actually do this:

    1) have he ball on a special layer, called "templates"

    2) have a blank layer called "new"

    3) create a new "ball" object on layer "new".

    4) use an event condition where I say ball.isOnLayer("new")

    5) then in the events actions, do this: ball.setLayer("target")

  • This event works only on the one created:

    <img src="https://dl.dropboxusercontent.com/u/48563442/Create-adjust.png" border="0" />

  • LittleStain

    good exmaple. Do you have to destroy the template object like this? I havent done this, which could be why in your third action its affecting mutliple sprites. Or perhaps because my equivalent of Sprite3 is in a family with lots of other copies of Sprite3?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Destroying should have no impact on the second event. Your problem probably lies somewhere else in your code.

    The family also shouldn't affect it unless your other actions are affecting the whole family instead of just the created object.

    I suspect you mean clones of sprite 3 instead of copies?

  • Here's the link to my capx

    I created a family in it just to try out if it would affect anything, but like I suspected it changes nothing.

  • As a follow up to LittleStains' example, can I use the created instance of Sprite3 in a sub-event?

    For example, can I have a sub-event that checks the created instance of Sprite3's position and perform actions based on that?

    Thanks.

  • moonraystudios

    Yes, you can. Best way to go about it might be to use 'On sprite3 created' trigger and do your stuff there.

  • Thanks.

    The sprite created trigger isn't available when I try to create a sub-event though.

    I can do that for a regular event, but then it will affect all created sprite3's, not the one I have just created.

  • Use an instance variable, set it to 0 for those sprites you don't want to be affected by the on created event, set it to 1 for those who should be affected. Then just add another condition to the on created event (instancevar = 1).

  • Awesome. Thanks.

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