Array of sprites

Get help using Construct 2

Post » Sat Dec 29, 2012 4:35 pm

Hi everybody.

I've tried searching in forum and in the manual, but I wasn't able to find what I was searching for...

Is there a way to have an array of sprite? Or, to change the perspective, is there a way to choose which element of a family has to be created?

My goal is to have an array of "template" and then being able to choose which one create... in pseudo code it will be something like:

i = Random(0,10)
SpriteToCreate = ArrayTemplate(i)

Can you suggest a way or a plugin that can do for me?

ThanksJeff Skyrunner2012-12-29 16:49:05
I'm not a insane, my mother had me tested

I'm an engineer. To save time, just assume I'm never wrong
B
29
S
12
G
4
Posts: 130
Reputation: 4,735

Post » Sat Dec 29, 2012 6:57 pm

You can set a variable from 0,10 then have sub-events like:

var==0 -> spawn a
var==1 -> spawn b
etc.

I just checked the manual and found this also,

General
Create object
Create a new instance of an object type on a layer at a given position. If a Family is chosen, a random object type from the family is picked, and an instance created for that.
B
25
S
3
G
6
Posts: 1,197
Reputation: 5,620

Post » Sat Dec 29, 2012 7:17 pm

Thanks for suggestion, Davioware, but that's not what I was searching for :)

As you said, creating a new istance of a Family, a random object will be created, but you don't know which one.
About the first solution, that is for sure the most straight one, but also the less "pure" one :) As a matter of fact, if you use that method, each time you add a sprite to your project for any reason (e.g. new release with new sprites, or different sprites depending of user, or whatever) you must add a new condition to the "switch case" and this will slow your algorithm, instead of picking the i-th istance of an array...

However, if there is no other solution, I will of course pick that one, thank you :)
I'm not a insane, my mother had me tested

I'm an engineer. To save time, just assume I'm never wrong
B
29
S
12
G
4
Posts: 130
Reputation: 4,735

Post » Sun Dec 30, 2012 12:15 am

You would have to add a new event with each new sprite you want, but how is that different than adding a new entry to the array?

If you're concerned that picking the i-th slot in an array would be marginally faster than checking a variable x amount of times you can go a bit further to "optimize it" to be more array like. Note the use of quotations to signify that it may or not be any faster. It might even be slower. Here's how:

you can use the function object, and have calls like:

on function "arrayslot1" -> spawn a
on function "arrayslot2" -> spawn b
etc.

then, to create your random object, or object of choice, you go:

i=random(10)
call function "arrayslot"&i
(& is the concatenation operator in construct)

This eliminates the need to check a variable x amount of times.
B
25
S
3
G
6
Posts: 1,197
Reputation: 5,620

Post » Sun Dec 30, 2012 12:42 am

Optimisation: don't waste your time.

Apart from the performance tips nothing really impacts performances code-wise.
Of course if you use major collision checks and physics use, it will have an impact, but as far as creating sprites from a loop, not that much.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,998
Reputation: 57,791

Post » Sun Dec 30, 2012 4:01 pm

Here's one way. Add the UIDs to the array and then Pick from a random index into the array.

PickRandomUIDfromArray.capx

Edit: Doh! I answered the wrong question. This helps for picking, not creating. Sorry. I don't see any way other than what has already been discussed. Each sprite has to be created explicitly.blackhornet2012-12-30 16:44:25
ImageImageImage
B
67
S
19
G
121
Posts: 3,196
Reputation: 72,879

Post » Sun Dec 30, 2012 5:38 pm

Thanks everybody for suggestions: maybe it's just my "old school programming head" that is worring me too much.

Thanks a lot.
I'm not a insane, my mother had me tested

I'm an engineer. To save time, just assume I'm never wrong
B
29
S
12
G
4
Posts: 130
Reputation: 4,735


Return to How do I....?

Who is online

Users browsing this forum: Baidu [Spider], MrLOL64 and 20 guests