Hi everyone, I've been using Gideros for a few days and I'm trying to add support for collision masks into a simple collision function but I'm having some trouble
I have a class, RedSquare, and I want to add a child, maskSprite, so I'm doing it like this
RedSquare = Core.class(Sprite)
local maskSprite
function RedSquare:init()
local bmp = Bitmap.new(Texture.new("redsquare.png"))
bmp:setAnchorPoint(0.5, 0.5)
self:addChild(bmp)
local mask = Bitmap.new(Texture.new("redsquare_mask.png"))
mask:setAnchorPoint(0.5, 0.5)
maskSprite = Sprite.new()
maskSprite:addChild(mask)
self:addChild(maskSprite)
end |
When I run the project, it seems to work fine and the mask follows the main object wherever it moves
The problem is, when I want to get the X value of the mask in RedSquare's update function (so I can pass it to the collision function)
If I do this:
It just prints 0.... But I can see that the mask is definitely not at 0
I'm sure I'm setting up the sprite the wrong way somehow but I can't figure it out, can anyone help?
Thank you
- 'Rio
Comments
you should try localToGlobal or local x,y,width,height = masksprite:getBounds(stage) to get the position of masksprite in actual screen or simply use the position parent sprite
I'm having the same issue but in my case I tried all the way possible and nothing happens. I have a sprite that starts with initial positions, but on mouse event I can change its position to whatever I want. This sprite is initiated on level class. Later, enemy is initiated, and I've set the sprite as parameter to get the values but indepently what I do, my enemy sprite receives always the sprite initial positions, never update its value... I have no idea of what this might be... If anyone has any life is appreciated.
Likes: Paulo777
Fragmenter - animated loop machine and IKONOMIKON - the memory game
either you should add the enemies to the ship instead of to the stage as child. that's simplest.
alternatively, which could be better in some use cases, you should make an empty table inside the ship init function, like self.enemies. and then every time you create a new enemy you should add it to this table as well (you can have a function Ship.addEnemy(enemy) e.g. for this and then call this function).
finally, in the ship event function you should update along with
self:setPosition(self.x, self.y) also the position of every enemy in the table like
Likes: Paulo777
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Likes: Paulo777
https://play.google.com/store/apps/developer?id=razorback456
мій блог по гідерос https://simartinfo.blogspot.com
Слава Україні!
It really makes sense when we think about objected oriented... about relationships between them... At first I'm thinking about "level", and what happens to the level, but I must take into consideration the relation between objects...
By the way, do you know where I can find sprite hierarchy explanation or material? If anyone knows anything or some link about I'd be very glad.
Likes: keszegh
https://docs.google.com/document/pub?id=149g_P1YlE4_6v_hHbFx3YAaYpdU0HoM70XJSLTE38ww
https://play.google.com/store/apps/developer?id=razorback456
мій блог по гідерос https://simartinfo.blogspot.com
Слава Україні!
"unpleasant behaviour... the enemy is added "glued" with the ship"
these seem to be contradicting each other, so i'm not sure what you want to achieve. try to clarify.
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Enemy init add:
self.ship=sprite
and then any time the enemy can get the current position of its mothership by querying
self.ship:getPosition()
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Likes: Paulo777
Likes: keszegh, antix
sprite1.mySprite=sprite2
and sprite hierarchy when you add a child to another like
sprite1:addChild(sprite2).
the first (sprite1.mySprite=sprite2) just creates a reference for sprite2 so sprite1.mySprite points to the same object as sprite2, and that's it, there is no other connection between them.
in the example self.ship=sprite, self refers to the sprite whose function we are inside currently (an object from the class Enemy), and self.ship is just a way to store sprite in the scope of this enemy.
the second example on the other hand ties sprite2 to sprite1 in the visual hierarchy, so whenever sprite1 moves/scales/is added or removed from stage/made invisible etc. then the same happens with sprite2.
sprite2:getParent() will still get you sprite1, so there is a way to get it (like in the first way), but the connection is now much stronger as i said, they basically move together.
to understand the first, probably you should read through a lua manual, the basics are short. for the second, the ultimate guide for gideros is a good place to learn about sprite hierarchy etc. but this concept is quite common, as actionscript works the same way.
Likes: Paulo777
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Likes: Paulo777
https://play.google.com/store/apps/developer?id=razorback456
мій блог по гідерос https://simartinfo.blogspot.com
Слава Україні!
@oleg not quite. But from now just simple behaviors like moving and shooting, as it is a side scrolling, more of them are in my plans to add in the level