Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Sprite:setAnchorPosition() tween/lerp Help please - Gideros Forum

Sprite:setAnchorPosition() tween/lerp Help please

MoKaLuxMoKaLux Member
edited July 10 in General questions
dear all, do you know if sprite:setAnchorPosition() can be tweened (lerped) for smooth transition (animation)?
-- camera testXX
local function lerp(a, b, t)
	return a + (b-a) * t
end
-- camera
local camera = Sprite.new()
local bg = Sprite.new()
local mg = Sprite.new()
camera:addChild(bg)
camera:addChild(mg)
stage:addChild(camera)
-- lerping switch
local camswitch = true -- SPACE BAR
local x, x2 = 0, 0 -- lerp between those 2 values
local l = 0 -- lerping value?
-- the scene
camera:setPosition(96, 96)
local pixelbg = Pixel.new(0xff00ff, 1, 64 * 8, 64 * 3)
pixelbg:setPosition(256, 256)
local playerA = Pixel.new(0x0, 1, 64, 64)
playerA:setPosition(256 + 32, 256 + 64)
local playerB = Pixel.new(0x00ff00, 1, 64, 64)
playerB:setPosition(256 + 32 * 4, 256 + 64)
bg:addChild(pixelbg)
mg:addChild(playerB)
mg:addChild(playerA)
 
function onKeyUp(e)
	if e.keyCode == KeyCode.SPACE then
		camswitch = not camswitch
		print(camswitch)
	end
end
stage:addEventListener(Event.KEY_UP, onKeyUp)
 
function onEnterFrame(e)
	x, y = playerA:getPosition()
	x2, y2 = playerB:getPosition()
	if camswitch then
		l = lerp(x, x2, 0.1)
		camera:setAnchorPosition(l, y)
--		camera:setAnchorPosition(x, y)
	else
		l = lerp(x2, x, 0.1)
		camera:setAnchorPosition(l, y)
--		camera:setAnchorPosition(x2, y2)
	end
end
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
In the sample project (which you can copy/paste to test) I am trying to make a smooth transition when I change the setAnchorPosition().

I found out, after days and days, that setAnchorPosition() can perfectly fit my needs, the only thing missing is a nice tween (lerp?). Is it possible?

Thank you for your help :)
EDIT: I have edited the code
my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
Tagged:
camera01.png
569 x 358 - 7K

Comments

  • rrraptorrrraptor Member
    edited July 10 Accepted Answer
    You need to get camera's anchor position first, then use "camera:setAnchorPosition(...)"
    function onEnterFrame(e)
    	local camX, camY = camera:getAnchorPosition()
    	camX = lerp(camX, dstX, 0.01)
    	camY = lerp(camY, dstY, 0.01)
    	camera:setAnchorPosition(camX, camY)
    end
    Also, MovieClip or GTween can be used aswell.
    local pix = Pixel.new(0x00ff00, 1, 100, 100)
    pix:setPosition(100,100)
    stage:addChild(pix)
     
    --local mc = MovieClip.new{
    --	{1, 100, pix, {anchorX = {0, 100, "outBack"}}}
    --}
     
    local gt = GTween.new(
    	pix, 2, {anchorX = 100}
    )

    Likes: MoKaLux

    +1 -1 (+1 / -0 ) Share on Facebook
  • MoKaLuxMoKaLux Member
    edited July 10
    thank you very much (at)rrraptor, I am going to try this right now :) I need to try this and then update the wiki :* DONE
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • rrraptorrrraptor Member
    edited July 10 Accepted Answer
    MoKaLux said:

    thank you very much (at)rrraptor, I am going to try this right now :) <3
    I totally missed the anchorX parameter for the movieClip :o </p>

    One more thing. You can create "set" function for your class to tween any value you want.
    local Foo = Core.class(Sprite)
     
    function Foo:init()
    	self.myProperty = 0
    	self.myValue = 0
     
    	self:addChild(Pixel.new(0,1,10,10))
    end
    --
    function Foo:myFunction(value)
    	self.myValue = value
    end
    --
    function Foo:set(prop, value)
    	local v = self[prop]
    	if (v) then 
    		if (type(v) == "function") then 
    			v(self, value)
    		else
    			self[prop] = value
    		end
    	end
    end
     
    local bar = Foo.new()
    bar:setPosition(100,50)
    stage:addChild(bar)
     
    local mc = MovieClip.new{
    	{1, 200, bar, {
    		myProperty = {0, 100, "outBack"}, -- tween variable
    		myFunction = {0, 100}, -- tween another variable but using this function
    		x = {0, 100, "outBack"}}
    	}
    }
     
    local tf = TextField.new(nil, "", "|")
    tf:setScale(2)
    tf:setPosition(100,100)
    stage:addChild(tf)
    stage:addEventListener("enterFrame", function()
    	tf:setText(bar.myProperty .. "\n" .. bar.myValue)
    end)

    Likes: MoKaLux

    +1 -1 (+1 / -0 ) Share on Facebook
  • MoKaLuxMoKaLux Member
    edited July 10
    mister rrraptor, this is working great (almost perfectly). I am using the movieclip function. Now I need to pick the right timing and easing.

    Thank you once again. I will upload a demo soon to show my progress (liquidfun platformer).

    PS: I want to keep the code as simple as possible for now :o
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
Sign In or Register to comment.