whats wrong in this loop?

Discussion and feedback on Construct 2

Post » Fri Feb 07, 2014 12:30 am

wanted to do a quick loopexample, and found something strange
if you run this group under a startcondition, it works fine
(the loopindexes adjust the sprite position like expected)
= examplefile

if you run this group after start on its own, (drag group out of start)
the positions are wrong, (but loopindex is still counted)

can someone check this, maybe im doing something wrong

https://dl.dropboxusercontent.com/u/61666915/looptest_group.capx
ImageImage
B
71
S
22
G
7
Posts: 827
Reputation: 10,177

Post » Fri Feb 07, 2014 1:08 am

I think it's because when you run the group after a delay there are no instances of Sprite available for the create instance expression to reference when setting the spacing using Sprite.Width and Sprite.Height. It works on layout start because there's an instance that can be referenced before it's destroyed at the end of the start layout tick. Or something like that.... Colludium2014-02-07 01:09:45
A big fan of JavaScript.
B
76
S
20
G
74
Posts: 2,255
Reputation: 46,484

Post » Fri Feb 07, 2014 1:40 am

@vtrix, I believe @Colludium is correct.
I dragged the group out of the On Start of Layout section and got the incorrect spacing as you described.

So, I disabled the line "Sprite : Destroy", and then the sprites were spaced correctly.

Then I changed the Create object line to just create the Sprite on layer 0 at (0,0)
and on the next line Sprite : Set position to ( 300+ loopindex("y") * (sprite.Width+1), 300 + loopindex("x") * (sprite.Height+10) )

and that worked fine (even with the "Sprite : Destory" line re-activated). So, basically, it needs to already have a Sprite instance available to get its width and height. You just have to divide the line to create the Sprite first, then position it second rather than do it all on one line...AllanR2014-02-07 01:46:32
B
98
S
35
G
20
Posts: 438
Reputation: 17,875

Post » Fri Feb 07, 2014 2:33 am

thats actually pretty strange, as i was under the impression, that once the object default properties are known, at the beginning of the runtime, you can destroy it, isn't this why you need to place an instance in the layout?





ImageImage
B
71
S
22
G
7
Posts: 827
Reputation: 10,177

Post » Fri Feb 07, 2014 2:36 am

I've tried both ways and they both place in the same location. Maybe post both of you files to see if there is something else subtly going on. You can confirm the size easily by adding the Browser object and logging the width/height. The values are 15 & 30, before and after the destroy.
ImageImageImage
B
71
S
22
G
249
Posts: 3,764
Reputation: 138,579

Post » Fri Feb 07, 2014 3:22 am

@vtrix. As far as i understand, the Height and Width values of a sprite don't exist in system memory if the sprite.count = 0. You can't access the Width and Height values until an object has been created, and C2 doesn't remember all of the past values of an object's parameters unless it's IID 0. Here's what I mean:

capx

I've always taken the need to create objects in a dump layout just to be a quirk of C2. I have made the mistake in the past of deleting instance zero of an object from my dump layout only to then have the 'default' spawn settings subsequently go crazy - size and angle - with no easy fix because I couldn't insert a new object with a lower IID than zero...Colludium2014-02-07 03:42:22
A big fan of JavaScript.
B
76
S
20
G
74
Posts: 2,255
Reputation: 46,484

Post » Fri Feb 07, 2014 3:32 am

@blackhornet , i just updated to latest beta version,

you mean they both work for you?

this is the link to the one not working
https://dl.dropboxusercontent.com/u/61666915/looptest_group_fail.capx

working
https://dl.dropboxusercontent.com/u/61666915/looptest_group_works.capx
ImageImage
B
71
S
22
G
7
Posts: 827
Reputation: 10,177

Post » Fri Feb 07, 2014 3:49 am

Ah, I did misunderstand what you were doing. Colludium is correct. If you destroy the object, you can't then get properties from it. Either do what AllanR suggested, or Colludium, or store the values prior to destroying.blackhornet2014-02-07 03:50:37
ImageImageImage
B
71
S
22
G
249
Posts: 3,764
Reputation: 138,579

Post » Fri Feb 07, 2014 4:05 am

now i know what my confusion is, because, if you do the same thing,
destroy sprite, and not at start..

sprite action , set width to sprite.width it works..

so, the sprite objecttype does have its width saved somewhere, i guess its only available i you select the sprite.object directly

okey, thanks for checking!
vtrix2014-02-07 04:10:20
ImageImage
B
71
S
22
G
7
Posts: 827
Reputation: 10,177


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 5 guests