I have number of objects that I added to stage, Now, out of them, there are couple of objects that I want to group, so If my player collides with any of them , I can respond to that. So, I believe all those objects are to be grouped?
If I already have added them to stage, do I need to add a line of code to add them to a group and so add group to the stage, or is there any possible alternative ?
Please let me know if there is any preferred way for achieving the same.
Thank you GIderos
Comments
grouping example:
https://deluxepixel.com
Likes: SinisterSoft
something like this:
self.anim{
bitmap:getTextureRegion("1.png"),
bitmap:getTextureRegion("2.png"),
and so on
}
so, how to pass this object as parameter?, please?
Secondly, this group1(in my code) contains objects that are created inside a class, so using Sprite: CollidesWith(secondsprite), using Sprite as first argument, will it work?
You can only check collisions with Sprite objects and classes which inherit from Sprite, as Bitmap, TextField, etc.
And you can't check collision between whole groups, you need to specifically check for each Sprite, by looping through the groups
But texture pack are set to Bitmaps as texture regions or a MovieClip
You need to check collisions between the renderable objects as Bitmaps and not Texture packs or texture regions
Maybe if you could post more code, it would be easier to understand what you want to achieve
So, here is the code that I have to render the images from Texture Pack, I meant to refer to images from texture pack only,(sorry for teh confusion)
local pack = TexturePack.new("Charecter.txt", "Charecter.png")
self.anim = {
Bitmap.new(pack:getTextureRegion("f1.PNG")),
Bitmap.new(pack:getTextureRegion("f2.PNG")),
Bitmap.new(pack:getTextureRegion("f3.PNG")),
--Bitmap.new(pack:getTextureRegion("l1.PNG")),
--Bitmap.new(pack:getTextureRegion("l2.PNG")),
--Bitmap.new(pack:getTextureRegion("l3.PNG")),
Bitmap.new(pack:getTextureRegion("b1.PNG")),
Bitmap.new(pack:getTextureRegion("b2.PNG")),
Bitmap.new(pack:getTextureRegion("b3.PNG")),
--Bitmap.new(pack:getTextureRegion("r1.PNG")),
--Bitmap.new(pack:getTextureRegion("r2.PNG")),
--Bitmap.new(pack:getTextureRegion("r3.PNG"))
}
self.frame = 1
self:addChild(self.anim[1])
self:setPosition(220, 50)
and now I have other objects that I have added as images on the stage and want to check if any of them collide with the above mentioned Character , so my question is how do I check if self.anim mentioned above is colliding with any of the other objects?
P.S -> The images in the self.anim are just that of a single character which change with time to render motion of the Charecter
That would actually be quite inefficient.
The default way would be to create animations using MoveClip:
http://docs.giderosmobile.com/reference/gideros/MovieClip#MovieClip
Which would basically add/remove Bitmaps for you
Alternative ways would be to create single Bitmap obejct and change in textures using setTexture or setTextureRegion
Another alternative would be using TNT Animator Studio: http://www.tntparticlesengine.com/?page_id=31
In all of these cases, there will be single instance (either MovieClip or Bitmap or TNT Animator class) which would represent the main character, and which can be used for collision check using collidesWith method
function Sprite:collidesWith(sprite2)
local x,y,w,h = self:getBounds(stage)
local x2,y2,w2,h2 = sprite2:getBounds(stage)
return not ((y+h < y2) or (y > y2+h2) or (x > x2+w2) or (x+w < x2))
end
for i = 1, gourp1:getNumChildren() do
local sprite = scope:getChildAt(i)
if sprite:collidesWith(someOtherSprite) then
--they collide
end
end
With normal Lua, would this be faster than getting the bounds locally to the main routine rather than having a subroutine (function) in this case? Does Lua somehow cache the functions?
Also what would be the case with LuaJIT ?
https://deluxepixel.com
In LuaJIT there are lots of optimizations underneath, and I think it actually runs your code and generates optimized native code by rearranging command sets, so this kind of stuff should be optimized by LuaJIT automatically.
Are all the character images the same dimensions?
if yes, then all you have to do is to always take the first Bitmap and use it to check the collisions:
Then you can use this variable to access current Bitmap:
Here is teh relative piece of code
cute = dummy.new()
stage: addChild(cute)
for i = 1, groupA:getNumChildren() do
local sprite = groupA:getChildAt(i)
if cute.anim[1]:collidesWith(sprite) then
youLoose()
end
end
function sprite1:collidesWith(sprite2)
local x,y,w,h = sprite1:getBounds(stage)
local x2,y2,w2,h2 = sprite2:getBounds(stage)
return not ((y+h < y2) or (y > y2+h2) or (x > x2+w2) or (x+w < x2))
end
And in this init.lua file, you need to put exactly this methods:
Can you post more code or maybe share a project sample for review?
http://giderosmobile.com/forum/discussion/4908/how-to-get-updated-position-of-x-and-y-when-position-is-changing-with-time#Item_1