Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Creating groups in Gideros — Gideros Forum

Creating groups in Gideros

simransimran Member
edited May 2014 in General questions
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 :D

Comments

  • ar2rsawseenar2rsawseen Maintainer
    Hello @simran
    grouping example:
    local group1 = Sprite.new()
    local group2 = Sprite.new()
    stage:addChild(group1)
    stage:addChild(group2)
     
    group1:addChild(Bitmap.new(Texture.new("image1.png")))
    group1:addChild(Bitmap.new(Texture.new("image2.png")))
     
    group2:addChild(Bitmap.new(Texture.new("imagea.png")))
    group2:addChild(Bitmap.new(Texture.new("imageb.png")))
  • simransimran Member
    @ar2rswseen: Thank you , but how can I get width, height, position of all the objects added in a group since I want to detect their collision with a character?
  • SinisterSoftSinisterSoft Maintainer
    You would have to loop through the children of group1 or 2 and check their x,y,w,h against the x,y,w,h of your character, break out of the loop when you find a collision.

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • simransimran Member
    @SinisterSoft : Oh, Thanks a ton! But, do I access them in usual way, like group1.member1 or just address them using member names only?
  • ar2rsawseenar2rsawseen Maintainer
    method to check for collision, put it somewhere in the project, for example in init.lua file:
    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
    then use it while looping through group
    for i = 1, gourp1:getNumChildren() do
    	local sprite = scope:getChildAt(i)
    	if sprite:collidesWith(someOtherSprite) then
    		--they collide
    	end
    end

    Likes: SinisterSoft

    +1 -1 (+1 / -0 )Share on Facebook
  • simransimran Member
    edited May 2014
    Thanks a ton, I am sorry for being a noob, but actually my object that is colliding is
    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?


  • ar2rsawseenar2rsawseen Maintainer
    @simran so bitmap in your context is actually a TexturePack?

    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
  • simransimran Member
    edited May 2014
    Oh, but one might be having hundreds of images in a sprite sheet, so what do we do in that case, generally players in games are a set of sprite sheets only and indeed we need to check the player's collisions with any of the objects, what is the approach for that?
  • ar2rsawseenar2rsawseen Maintainer
    @simran what do you mean by spritesheet? a texture pack?

    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 ;)
  • simransimran Member
    Okay,
    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
  • ar2rsawseenar2rsawseen Maintainer
    edited May 2014
    ah, I see, so that actually relate to a single character. Well I assume you animate it by adding/removing those bitmaps from stage or scene?

    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
  • simransimran Member
    Oh, okay :) Thanks a ton ! BUt still I would like to know if it is possible to use collides with using the same texture region technique, Is it?
  • SinisterSoftSinisterSoft Maintainer
    @ar2rsawseen

    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 ?
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • simransimran Member
    @ar2rsawseen: Yes, I add them to the stage for creating the animation after checking a couple of conditions, but I now so desperately want to know if it is possible to use collideswith with this same programming style, please? :D
  • ar2rsawseenar2rsawseen Maintainer
    @SinisterSoft it would be faster to cache the bounds of the character you are testing on others yes. Definitely in plain Lua.
    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.
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    @simran
    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:
    self.anim[1]:collidesWith(otherSprite)
    If not then, you probably save the current image index in some variable, so you would know which animation to switch to next, right?

    Then you can use this variable to access current Bitmap:
    self.anim[variable]:collidesWith(otherSprite)
    You also mentioned that they are in separate classes, but if they are stored as instance properties as:
    dummy = Core.class(Sprite)
     
    function dummy:init()
        self.anim = {...}
    end
    Then you can also access them as instance properties:
    local d = dummy.new()
    d.anim[variable]:collidesWith(otherSprite)
  • simransimran Member
    @ar2rsawseen : Thank you ,thank you ,thank you so much .Good day :)
  • simransimran Member
    Hey, so it still says, attempt to call colldieswith (a nil value)

    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
  • ar2rsawseenar2rsawseen Maintainer
    @simran you should create init.lua file in your project (init.lua with exactly this name, this file will be special, because it will be the first one to execute)

    And in this init.lua file, you need to put exactly this methods:
    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
    It will add collidesWith method to any obejct that is Sprite or Sprite inherited instance
  • simransimran Member
    @ar2rsawseen: Okay, thanks a ton, I was completely unaware of this init.lua thing that it is always the first one to be called, so I did exactly the same as suggested and it did not give an error, but it still does not detect the collision. I have attached the image, in the image the clock hands are over which we have the loop and the character is self.anim, but it just crosses it and it never detects the collision, what seems wrong here?I am sorry again :/
    img.PNG
    300 x 496 - 157K
    img.PNG 156.8K
  • ar2rsawseenar2rsawseen Maintainer
    Really hard to tell what is not working. Are you sure you check for collision for right obejcts and recheck it on every enter frame event?

    Can you post more code or maybe share a project sample for review?
  • simransimran Member
    actually, the positions of both are changing at may be less than a second and what we were using in above example, the values were never updated
Sign In or Register to comment.