I don't get how the sprites are created

Discussion and feedback on Construct 2

Post » Sat Nov 26, 2016 6:07 am

Hi!

Not posting this in bugs, as it surely ain't one, but maybe more a weird behavior....

Let's make things simple. Open a new project, create one sprite, draw an arrow left looking, place it on your layout.

Image

Add an event in your sheet to create one more arrow

Image

The result is, without doubt:

Image


Now, mirror the manually placed arrow by using a negative width. (Ban negative width? Give me another way to do it without doubling my frames and being able to see the result on the editor, meaning not having to preview the layout to see the end result)

Image

Without changin anything in the event sheet, just preview. Weirdly, the result is:

Image


Now, I say weirdly, why? Maybe it's wanted, but you have to explain it to me then. Programmatically thinking, this looks to me like a static variable conflict. Why? On the right side, we have our Project tree, with our layouts, sheets, and objects. When dragging an object in the layout, we instanciate it, create an instance of it. Thus, all it's attributes are set to the object's default. When we create through code, we do instanciate it too. If it's not that, then the whole idea is really looking like that and so are the guides talking about instances, the functions referencing to the "instance" word etc etc and that really needs semantic fix then

So, by looking how Instance Variables and Common work, to me it looks like "Layer", "Angle", "Opacity", "Position", "Size" are declared as static in the object (where instance variables are what they are called, non static instance properties), explaining why, after setting one object's width to negative, it spreads and infects the original object. I know the category for those is called "Common", that's a sign.

It is also easily understandable by looking at the .caproj file:

Image

Nothing references the base width, height etc etc

So, anyone has a solution for me to not have my sprites flipped by default when code created if I manually placed one of them and reversed it?. Sorry, the project is very wide, and I won't add setMirrored = false, setFlipped = false and setScale 1 after every sprite creation throughout the many event sheets and sprite creations....

Cheers!
B
32
S
11
G
3
Posts: 214
Reputation: 4,267

Post » Sat Nov 26, 2016 6:34 am

The first sprite is alway a template for all others. If you deleted that sprite from the layout itself, you'll find C2 can't create any more sprites. It always needs one placed in some layout somewhere with the default settings. The common trick is to delete all default sprites in your layout and create new ones. The alternative is to place that one sprite in a utility layout that is never actually displayed. It's just one of the things you have to accept and deal with.
ImageImageImage
B
71
S
21
G
231
Posts: 3,688
Reputation: 129,352

Post » Sat Nov 26, 2016 6:43 am

But but but, I do get that... But the instance vars aren't copied over either, so why aren't the width and Co non static also? Placing one sprite in a fallback layout is what we do too, but the ones created/manually placed in other layouts took over...
B
32
S
11
G
3
Posts: 214
Reputation: 4,267

Post » Sat Nov 26, 2016 6:50 am

What I did to overcome this now is:

Add to those sprites an instance variable called "layoutPlaced" with default to false.
When manually creating a layout and placing one of these sprites, we set it to true
In a super sheet, I do "onCreated / if not layoutPlace setScale 1 setMirrored false setFlipped false"

This way, all the code generated sprites do fall into that condition and get defaulted where all the manually placed ones stay as we did place them
B
32
S
11
G
3
Posts: 214
Reputation: 4,267

Post » Sat Nov 26, 2016 6:28 pm

Exactly what blackhornet said. The first sprite created is the main sprite that all instances copy default atributes from. If you notice, in your example, if you change the right sprite's width instead of the left one, only it will rotate.

When you tell C2 to "create a new object" it really just copies the object from this first instance to use on the others.

Now, instead of coding you can just create a new layout called "repository" (or whatever you want it to be called) and have the first instance of your sprites be there (to be sure they're the first instance you'll have to delete all other instances from your project). I do this for all my games and I always recommend it.

After that you can just copy it to any layout you want, while editing it.

Alternately you can also pick objects by their IID (instance ID), meaning that in your example, the arrow on the left has IID = 0 and the arrow on the right has IID = 1. So if you do something like "pick sprite where sprite.IID = 0" it will pick the arrow on the left.
B
123
S
43
G
41
Posts: 847
Reputation: 28,206

Post » Tue Nov 29, 2016 2:18 pm

And how exactly is the first instance defined? Is it the one with the lowest UID? Because after a couple of tries, this is not the case...

I added one of the sprite on a placeholder layout, I manually opened the layouts xml file, made sure this new element gets the UID 0 by swapping with another one in another layout. Still, the problem persists. Even instance variables are copied over, that's totally not ok, they are not to be called instance variables in that case

Image
B
32
S
11
G
3
Posts: 214
Reputation: 4,267

Post » Tue Nov 29, 2016 3:17 pm

wait so you are saying that if you set the width of the arrow to negative width its not mirroring it anymore?

cause that is weird... i had made a card flip based on that and using the same inputs and it worked... you sure isn't something you have in the code that stops you from doing that? like setting the width and height to a surtain value everytick(or a Boolean that is always on which acts like everytick basically) ?


let me test it first though il be right back. im using R240 atm hope you can open it.
B
75
S
22
G
68
Posts: 1,337
Reputation: 43,271

Post » Tue Nov 29, 2016 3:23 pm

No, I'm saying that even if the very first created sprite in my project has the default width set positively and is scaled 1:1, the code generated ones spawn reverted. The code generated ones even take the instance variable values from another sprite placed somewhere else, dunno which, but not the very first one. So it's not taking the very first created sprite, but some random others
B
32
S
11
G
3
Posts: 214
Reputation: 4,267

Post » Tue Nov 29, 2016 3:36 pm

Psychokiller1888 wrote:No, I'm saying that even if the very first created sprite in my project has the default width set positively and is scaled 1:1, the code generated ones spawn reverted. The code generated ones even take the instance variable values from another sprite placed somewhere else, dunno which, but not the very first one. So it's not taking the very first created sprite, but some random others



ah yes now i see ...


i think was mentioned before...

i seen this problem myself also but its not really a problem but more of the ordinary things that where not needed to be fixed i guess cause seemed natural, and no1 complained/or everybody got used with it as in my case.


what i seen is this: when you first place your sprite object in a layout,that is usually deleted on start of layout unless its the main player and u actually use it from the visual editor placement,

anyway that very first sprite is affecting every other future code generated of same sprites for example if you first create a fish .. and on start of layout you delete it cause you dont need it...

then you go create a random number of them doesn't matter under what condition... the new fish created will be exact copies as the one that got destroyed upon removal/ start of layout cleanup/ destroyed 1st on start.

there is no way around it but ask Ashley here about this. or modify the way C2 engine works i guess. its a cache memory issue i supposed, or canvas cache memory history thing that its only saving the first state and form of a sprite.

however i always patch this thing by adding a new action under the new created sprites if i want them to be flipped immediately after its created.. i added a condition for it to be flipped or if that is to many lines... just add a new line of conditions similar to this code bellow

Code: Select all
Fish on created ---------------- action for size/scale
============================
           if someconditiontoflipison - set flipped/height
============================
============================
          if someconditiontomirrorison - set mirror/width
============================                           


since now it did not fail.

but you said its taking some random other... that makes me suspect of the browser cache you have ... do you use SaveGame System Function? or persistent/global objects?

Edited: i ask about any saving /caching issue because i seen if you save locally something and then try open a new capx version after you closed that or update a game on a website some cache remains are going to affect it.


Edit2: all that explanation above made basically the same point you said just without the error.... lol .... i feel stupid for not stay and read till i get it right.... yes if its not acting as i said above and as you said... then its a cache problem.. that means... your game if its hosted on a website... and u updated a new version of it... you need to clear the cache history media or the data hosted thing... not sure what counts now... but for me cleaning that in chrome was fixing it.
Last edited by gamecorpstudio on Tue Nov 29, 2016 3:45 pm, edited 1 time in total.
B
75
S
22
G
68
Posts: 1,337
Reputation: 43,271

Post » Tue Nov 29, 2016 3:42 pm

That's why I ask, what defines "the first one created".

As you can see in my screenshot above, the sprite InterfaceElement with UID 1 (uid 0 is something else, we don't care, it's another sprite) is set with positive width and scaled 1:1, placed on a placeholder layout. The layout that loads first also uses this sprite, for the interface, manually placed in layout. Some are normal, others are reversed. Then comes the code, that creates also a few InterfaceElements, but they are reversed and squeezed... So, my point is: What defines "first created element", because obviously it's not the one with the lowest UID

I am not going to delete all InterfaceElements sprites from my roughly 40 layouts, all together what, 500 InterfaceElements manually placed, and recreate everything. I will rather manually fix that in the xml files

I talk about local testing, no chaing issues (and browser has caching disabled for testing)
B
32
S
11
G
3
Posts: 214
Reputation: 4,267

Next

Return to Construct 2 General

Who is online

Users browsing this forum: lukewasthefish and 11 guests