Citrus-Engine icon indicating copy to clipboard operation
Citrus-Engine copied to clipboard

Curious about CitrusObject.as, whether it may be a little too light? here's why-

Open Snky opened this issue 9 years ago • 8 comments

Just a thought.. let's take a physics engine object and a citrussprite for example, if these two were stored in the same array, how would one access the x and y values via array[ i ].x/y? If the only common parent is CitrusObject.

Snky avatar Mar 06 '15 10:03 Snky

if these two were stored in the same array, how would one access the x and y values via array[ i ].x/y?

Just by making a cast? (myArray[0] as CitrusSprite).x

Also, to do it properly, we should cast using the interface ISpriteView. But it seems that APhysicsObjectdoesn't implement it. Any idea @gsynuh why it hasn't been done?

alamboley avatar Mar 06 '15 10:03 alamboley

If I were to write that, and there was a nape physics object in the array, wouldn't it error? ( cycling through the array via for loop )

Snky avatar Mar 06 '15 11:03 Snky

If your myArray[0]object was a Nape object, yes. The issue you're right, is that CitrusSprite & APhysicsObject just have CitrusObject as the parent. We should put the interface for both of them.

alamboley avatar Mar 06 '15 11:03 alamboley

Well, in fact NapePhysicsObject already implements ISpriteViewso everything works as expected!

Just use myArray[0] as ISpriteView).x and no matter if its a CitrusSprite or a NapePhysicsObject.

alamboley avatar Mar 06 '15 11:03 alamboley

Ah, alright, damn, I did not know I could use interface classes like that, I was always taught they were just to enforce a different class to follow that blueprint (interface) nothing more. Thank you

Snky avatar Mar 06 '15 11:03 Snky

can the common properties in the interface class be set? As in: (myArray[0] as ISpriteView).visible = false , as there are no setter functions, thanks for your time.

Snky avatar Mar 06 '15 11:03 Snky

yes visible can be set.

actionscript interfaces does not allow us to force anything other than methods, so to say that any ISpriteView object should have a "visible" property, we define the getter as a workaround. So at least its going to be a readable property but in reality they can be set as well (edit: you can see "view" is in there as well)

gsynuh avatar Mar 06 '15 11:03 gsynuh

Come to think of it, I'd totally understand if this line threw an error because there's no explicit set defined in the interface - and in that case we'd have to add it

gsynuh avatar Mar 06 '15 11:03 gsynuh