create objects dynamically

0 favourites
  • 12 posts
From the Asset Store
Fully commented source code/event sheet & sprites to create a space shooter game
  • how do i create

    say 35 objects(sprites that act as buttons)

    and controlling its state(selected/clicked)?

    im thinking to do a for loop 35 times and create objects then i want to store the any action in an array so i know that number 5 was clicked

    but i cant seem to find a way of creating objects with names i can use dynamically, so that in another loop i can check value of each button

    (each button should have the name i(number from loop),

    so far i've created a sprite,a textbox and made them into a container.

    now i need to find a way to render them in a loop, but each button should be controlled separately,

    yes its easy to do manually but if there is a way to make it programatically, im all for it.

  • hahahah, this is exactly what I'm looking for in the post I made.

    You can check my CAPX (I went ahead on the manual mode) :P

    Because I couldn't figure it out either

    scirra.com/forum/topic76631_post458784.html

  • Could you use one sprite with 35 animation frames, then use the animation frame as the identifier - they could then be created in a loop. You could even pass the animation frame to the array.

  • Just thinking out loud:

    Would it work using a local variable which you add 1 to in each loop and set an instance-variable to that local variable on creation?

  • im not sure what you mean, but i dont think so based on the fact that i cannotuse the variable to get the container

    i need to link the variable i to the container so that i can read and modify the container

  • You want to be able to set a variable, but you can't use the variable?

    I don't see the logic..

    If every object in the same container has the same instance variable, wouldn't that be usefull?

    If you would use a local variable that is a number you could set a text-variable by setting its text to: "i"&localvariable

    so for the first one the text variable would be "i1", the second "i2", etc..

    But it seems I'm not understanding your question..

    And I'm even starting to question my own logic, haha..

  • Piotr,cool!

    do you mind spilling the beans?

    I couldn't find anything on your link that pertained to this problem.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You want to be able to set a variable, but you can't use the variable?

    I don't see the logic..

    If every object in the same container has the same instance variable, wouldn't that be usefull?

    If you would use a local variable that is a number you could set a text-variable by setting its text to: "i"&localvariable

    so for the first one the text variable would be "i1", the second "i2", etc..

    But it seems I'm not understanding your question..

    And I'm even starting to question my own logic, haha..

    but i cannot access the object by "i1"..

    so setting a instance variable doesn't help me making 35 unique buttons

    I need to be able to make button 10 to light up, i need to catch input like button 35 was pressed.

  • This event should do it:

    -on button clicked

    -button instance variable is "i10" ---> button light up

    instance variables are a perfectly fine way to see which instance of an object is being interacted with in my opinion.

  • to clarify the purpose of this application:

    I am making a lottery picker where you select 4 numbers from 1 to 35 and press go, upon go I make a ajax request to my server who will respond with the winning combination, then a wining animation will be displayed on those winning numbers.

    .

    to make 35 buttons and have them highlighted when clicking is no problem but now I muust take result from server and generate selected winning numbers animation but how can I reach these objects who are all instances but have an instance variable (between 1 and 35 as identifier)

    How do I capture an object based on instance variable?

  • What is the action you want to perform and what is the event that triggers that action?

    Instead of using the name you use the instance variable.

    Even simpler would be to make a function and add the variables as parameters.

    So the winning numbers you get from your ajax request will be send to a function. This function will use those numbers to select the right buttons to play the animation.

    You could also just choose to use the idea sainsy gave. Use animationframes for the different numbers and check those against the winning numbers from your ajax request.

  • It isn't that hard to do if you understand what you are doing.

    system compare two values - Sprite.instancevariable = winning number - sprite set animation to "win"

    Okay, you'd need a for each here to not have every sprite go to the animation win, but these are the basics.

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