Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

tween.sequence, intended behavior question

edited October 2017 in Questions Posts: 121

I spent two hours trying to figure out the problem in my code before i tried seeing if it was a problem with tween.sequence. I was expectng it to tween from soul.xy to the target, instead it set soul.xy to meow.xy first, then went to the target

-- dr
--tiny snippet of example code that reproduces the 'bug'
-- Use this function to perform your initial setup
function setup()
    meow={x=1,y=1}
    a=tween(.125, meow, {x=2,y=700})
    b=tween(.5, soul, {x=75,y=150}, tween.easing.linear)
    tween.sequence(a,b)

end

soul = {img="Project:heart",x=512,y=384}

function draw()
    background(0, 0, 0, 255)
    tint(255,0,0)
    --sprite(soul.img, soul.x, soul.y, 25,24)
    ellipse(soul.x, soul.y, 25)
end


Comments

  • dave1707dave1707 Mod
    Posts: 7,468

    @xThomas Here’s an example of tween.sequence.

    displayMode(FULLSCREEN)
    
    function setup()
        cb={x=50,y=50}
    end
    
    function draw()
        background(40, 40, 50)
        fill(255)
        text("tap screen to start tween",WIDTH/2,HEIGHT/2)
        sprite("SpaceCute:Health Heart",cb.x,cb.y)    -- draw sprite
    end
    
    function touched(t)
        if t.state==BEGAN then
            move()
        end
    end
    
    function move()
        t1=tween(2,cb,{x=WIDTH-50,y=HEIGHT-50},tween.easing.linear)
        t2=tween(2,cb,{x=WIDTH-50,y=50},tween.easing.linear,tweenDone)
        t3=tween(2,cb,{x=50,y=HEIGHT-50},tween.easing.linear,tweenDone)
        t4=tween(2,cb,{x=50,y=50},tween.easing.linear,tweenDone)
        t5=tween(2,cb,{x=WIDTH/2,y=HEIGHT/2},tween.easing.linear,tweenDone)
        tween.sequence(t1,t2,t3,t4,t5)   
    end
    
  • Posts: 424

    @xThomas I think you are right. Looking at tween.lua, when a tween finishes then the following code is invoked:

    local function finishTween(self)
      --copyTables(self.subject, self.target)
      self.running = self.time
      easeWithTween(self, self.subject, self.target, self.initial)
      if self.callback then self.callback(unpack(self.args)) end
    
      tween.stop(self)
    
      if self.next then
        self.next.initial = copyTables(self.next.initial, self.target, self.subject)
        tweens[self.next] = self.next
      end
    
    end
    

    In a tween.sequence, each tween has a field .next set to the next tween in the list. The line:

    self.next.initial = copyTables(self.next.initial, self.target, self.subject)
    

    copies over any data from the just-run tween to the next one, so in your example, the x and y are copied from meow to soul as the soul tween starts (sticking in a parameter.watch("soul.x") shows that this happens just as the first tween ends).

    I'll agree that this is not made clear in the documentation, and the given example hides this in that all the tweens are on the same table.

    I'll reserve judgement as to whether or not this is the intended outcome or not, but in case it says as status-by-design then you can achieve what you want via callbacks:

    tween(.125,meow, {x=2,y=700}, nil, function() tween(.5, soul, {x=75, y=150}, tween.easing.linear) end)
    
Sign In or Register to comment.