Set_hflip works only once (SOLVED)


As I experiment with Defold I have run into an issue which could well be down to my understanding of things. Here is what I do

  • I have four game objects set up with collisions and aligned on the four edges - top, left, bottom and right - of my stage (my own inherited terminology for gameworld)

  • In the init function of the script associated with these game objects I record their IDs which I store in my central repository for shared data which I call CONSTS - see my [A central repository for shared strings?](http://other question)

  • In the script associated with the sprites that populate this gameworld I have implemented the following code

     local CONSTS = require "scripts.statics"
     --imports by central repository into the present context
     function init(self)
      local signX
      local signY
      if (0.5 > math.random()) then signX = -1 else signX = 1 end
      if (0.5 > math.random()) then signY = -1 else signY = 1 end
      self.ownID = go.get_id()
      --record the sprite autogenerated ID
      self.speedX = signX * math.random(50,100)
      self.speedY = signY * math.random(20,100)
      --assign a speed in both the X & the Y directions
      -- signX/Y decides whether the sprite will move up/down left/right
      self.position = go.get_position() 
      --get initial position, assigned by the spawning factory
      self.flip = false --no need to flip right now
    function update(self,dt)
        local delta = vmath.vector3(dt*self.speedX,dt*self.speedY,0)
       self.position = self.position + delta
       --calculate the new sprite position 	
     if (self.flip) then  sprite.set_hflip(self.ownID,true) end
         --flip it if required
     self.flip = false
         --finished flipping
        --assign the new position
     function on_message(self, message_id, message, sender)
    if (message_id == hash(CONSTS.COLLIDED)) then 
    if (message.other_id == CONSTS.leftWallID) then wallBounce(self,true)
      elseif (message.other_id == CONSTS.rightWallID) then wallBounce(self,false)
     elseif (message.other_id == CONSTS.floorID) then floorCeilBounce(self,false)
     elseif (message.other_id == CONSTS.ceilID) then floorCeilBounce(self,true) end
     function wallBounce(self,offLeft)
    self.speedX = -self.speedX	
        --change tthe direction of travel in the horizontal direction
     self.flip = true
        --record the need to flip the sprite
     function floorCeilBounce(self,offCeil)
     self.speedY = -self.speedY

I find quite consistently that the first wall bounce results in the sprite flipping horizontally. However, all subsequent wall bounces do not result in a flip even though sprite.set_hflip(self.ownID,true) is being duly called. What am I getting wrong here



You need to alternate the value.

self.flipped = not self.flipped
sprite.set_hflip(self.ownID, self.flipped)
1 Like


Thank you!

1 Like