Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
graphics objects flickering when attached to body? — Gideros Forum

graphics objects flickering when attached to body?

RedpicmanRedpicman Member
edited February 2016 in General questions
Hi all,

when I create a circle physics object and by using applyForce() move it from the bottom to the top of the screen. The physics object moves smoothly as expected. When I attach a graphic to it, it also moves smoothly up the screen. The problem is however, the graphic flickers, and therefore looks faint. I guess this is happening for three reasons: 1) the physics object is moving about 5 pixels a step, and the graphic has to redraw itself evertime. 2) both the body and the graphic is quite small (maybe 5 to 6 pixels diameter). I have tried the graphic as a stand alone graphic and as part of a sprite sheet but I cant see no difference. 3)I have the step rate at 60 frames a second.

Are there any strategies to get the graphic to move without flickering. Another thing to note that this thing is created at runtime (it is a bullet) where as the other object that is created at the start of the level does not flcker when moved (but there again it moves slower and is bigger. I am moving the graphic to the body by using the object:setPosition() function. Is the optimum way of doing this?

I am sure there is an optimum way of doing this, so if anyone has ideas, I am all ears (and eyes).

Comments

  • antixantix Member
    edited February 2016
    In your EnterFrame code, are you getting the position of the physics body and setting the sprites position accordingly? Something like this..
      local body = physicsObject.body
     
      graphicObject:setRotation( body:getAngle() * (180 / math.pi) )
      graphicObject:setPosition( body:getPosition() )
  • Also, make sure you update the Sprite and Box2D in the same loop. If you use two separate loops, you sometimes get strange behaviour in my experience.

    Likes: antix

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 )Share on Facebook
  • Hi antix and totebo,

    Yes I think I am doing both of those things. Here is a cutdown version of the code that I am using (note I am using Scenemanager for level control)
    ----- level one set up--
    Level1 = Core.class(Sprite)
    function Level1:init()

    function self:fireBullet()
    local x,y = self.playerTank.tankBody:getPosition()
    self.bullet = Bullet.new(self,x,y)
    self:addChild(self.bullet)
    end



    function self:onEnterFrame()

    self.world:step(1/60, 8,3)
    self.playerTank:moveTank()
    self.playerTank:upDateImage()
    if(self.bullet) then
    self.bullet:updateImage()
    end

    end

    self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame,self)

    --- Bullet Class -------

    Bullet = Core.class(Sprite)

    function Bullet:init(lvl , objectX, objectY)
    print(objectX, objectY)

    local x,y = self.body:getPosition()
    self.body:applyForce(0,-5,x,y)

    self.bulletImg = Bitmap.new(Texture.new("images/bullet.png"))
    self.bulletImg:setAnchorPoint(0.5,0.5)
    self.bulletImg:setPosition(objectX, objectY - 20)
    self:addChild(self.bulletImg)

    self.xOffset , self.yOffset = self.bulletImg:getPosition()

    local circle = b2.CircleShape.new( objectX, objectY - 20, 2)
    self.body = lvl.world:createBody({type = b2.DYNAMIC_BODY})
    self.body:createFixture({shape = circle, density = 1.0, friction = 1.0, restitution = 0.2})



    function self:updateImage()
    --print("click")
    local Xbod,Ybod = self.body:getPosition()
    self.bulletImg:setPosition(Xbod + self.xOffset, Ybod + self.yOffset)

    end


    end

    end
Sign In or Register to comment.