Transition help

I have this line of code that make hands move across the screen… When its reached the other side of the screen I want them to move back from where they came from… So essentially what I am trying to accomplish is hands moving back and forth on the bottom of the screen…

With the code I have here they just stop after the first time…Does anybody know how to fix this?

[lua]local function moveHand()
local hands = display.newImageRect(“hands.png”, 200, 127)
hands.x = screenW + hands.contentWidth;
hands.y = 700
local moveSpeed = 5000
local moveRight = transition.to(hands, {time=moveSpeed, x= 550})
transition.to(hands, {time=moveSpeed, x=-50, onComplete=moveRight})
end[/lua] [import]uid: 51459 topic_id: 14062 reply_id: 314062[/import]

ok with some tests I was able to move the hand back but I don’t know how to loop the function can somebody help me with that…

[lua]local function moveRight (_t)
transition.to(_t, {time=moveSpeed, x= 1024})
end

local function moveHand()
local hands = display.newImageRect(“hands.png”, 200, 127)
hands.x = screenW + hands.contentWidth;
hands.y = 700
transition.to(hands, {time=moveSpeed, x=-50, onComplete=moveRight})
end[/lua] [import]uid: 51459 topic_id: 14062 reply_id: 51782[/import]

here you go
[lua]local box = display.newRect(0,0,50,50)
box.x = 0
box.y = 500
function transition_to_left()
transition.to(box, {time=2000, x = 0, y = box.y, onComplete = transition_to_right})
end

function transition_to_right()
transition.to(box, {time=2000, x = 300, y = box.y, onComplete = transition_to_left})
end

transition_to_right()[/lua] [import]uid: 16142 topic_id: 14062 reply_id: 51784[/import]

Thank you so much dark consoles!! Looks just pong!! [import]uid: 51459 topic_id: 14062 reply_id: 51788[/import]

Hey dark Console!! Can you help me figure out how I can tell the program to only dispatch the dispatcher when the hands are inside the hit spot that I have created? Heres the code that I have so far
[lua]display.setStatusBar( display.HiddenStatusBar ) – HIDE STATUS BAR

– LOAD PARTICLE LIB
local loqsprite = require(‘loq_sprite’)
local physics = require (“physics”)
local director = require(“director”)
physics.start()
physics.setGravity(0,0)

local rand = math.random

local moveSpeed = 4000

local screenW = display.contentWidth
local screenH = display.contentHeight

local BG = display.newImageRect(“background.png”, 1024, 768)
BG.x = screenW/2; BG.y = screenH/2

local gfactory = loqsprite.newFactory(‘girl’)
local girl = gfactory:newSpriteGroup()
girl.x = 508; girl.y = 443

local wfactory = loqsprite.newFactory(‘water’)
local water = wfactory:newSpriteGroup()
water.x = 300; water.y = 310
water.alpha = 0

local hfactory = loqsprite.newFactory(‘hands’)
local hands = hfactory:newSpriteGroup()
hands.x =130; hands.y = 460
physics.addBody (hands, {radius = sensorRect, isSensor = true})
hands.myName = “hands”
atrace(xinspect(hands:getSpriteNames()))

function moveLeft()
transition.to(hands, {time=moveSpeed, x = 130, y = hands.y, onComplete = moveRight})
end

function moveRight()
transition.to(hands, {time=moveSpeed, x = 870, y = hands.y, onComplete = moveLeft})
end
local hitSpot = display.newImageRect(“hitSpot.png”, 240, 150)
hitSpot.x = 510; hitSpot.y = 555
hitSpot.alpha = 0
physics.addBody (hitSpot, {radius = sensorRect, isSensor = true})
hitSpot.myName = “hitSpot”
local screenTouch = function( _e )
if _e.phase == “ended” then

dispatcher()

end
end

function dispatcher()
water:play(“water splash”)
water.alpha = 1
hands:play(“handmove splash”)
girl:play(“washFace cleanFace”)
end

function hitSpot:collision(_e)

if _e.other.myName == “hands” then
print(“hands Passed”)

end
end
hitSpot:addEventListener(“collision”, hitSpot)
Runtime:addEventListener (“touch”, screenTouch)
moveRight()[/lua] [import]uid: 51459 topic_id: 14062 reply_id: 52161[/import]

[lua]local screenTouch = function( _e )
if _e.phase == “ended” then
if (hands.x + hands.width/2 > hitSpot.x - hitSpot.width/2 and hands.x - hands.width/2 < hitSpot.x + hitSpot.width/2) then
dispatcher()
end
end
end[/lua]

that should work I think [import]uid: 19176 topic_id: 14062 reply_id: 52187[/import]

THANK YOU SO MUCH NE.HANNA!! THATS EXACTLY WHAT I WANTED!!! [import]uid: 51459 topic_id: 14062 reply_id: 52190[/import]

I just tested ne.hannah and it worked great!! Thanks again! [import]uid: 51459 topic_id: 14062 reply_id: 52193[/import]

how would you “stop” the transition ? I am looking to pressing a button and stopping the transition and then have that “object” turn into a “static” object. [import]uid: 11094 topic_id: 14062 reply_id: 61353[/import]

You can do something like this to stop it

[lua]Runtime:removeEventListener (“touch”, screenTouch)[/lua] [import]uid: 51459 topic_id: 14062 reply_id: 61374[/import]

You can also erase moving objects and put static ones in their place. The use case for this would be an explosion replace an enemy object. [import]uid: 10903 topic_id: 14062 reply_id: 61378[/import]

@crssmn

do you mean changing the alpha=0 on the transition.to ?
that just makes it “invisible”.

“You can also erase moving objects and put static ones in their place.”, do you mean like setting the object = nil ?

[import]uid: 11094 topic_id: 14062 reply_id: 61471[/import]

Nope I would do it one of two ways. Use timer.cancel() to stop the transition, or store the transitioning objects position, use display.remove() on it and then spawn a static object at the stored position.

However why are you using transitions in the first place? They tend to use a bit of memory and are not the best ways to move characters or enemies or most game objects around the screen. If are moving objects around the screen I tend to prefer using runtime listeners to move objects around. [import]uid: 10903 topic_id: 14062 reply_id: 61611[/import]

When I erase objects, I basically use the timer for the transition, and then do a remove on it like others are saying.

You can go with runtimelisteners as well as crssmn. I guess it depends on how much you have going on in the game.

Each situation is valid, it’s a debate on performance - if it runs great using transitions on a real device and you don’t plan on going nuts then cool. If it’s bogged down, then ya go with runtime.

:slight_smile:

ng

[import]uid: 61600 topic_id: 14062 reply_id: 61886[/import]

Heres an example of a transition using a runtimelisteners making the flies move from left to right… Theres also an example of how to set up a game over method!

http://howto.oz-apps.com/2011/10/bug-catcher-game-tutorial.html [import]uid: 51459 topic_id: 14062 reply_id: 61888[/import]