Starting and stopping a sprite?

My question is how can I click a button and make a sprite start moving?
Sprite Code:

 local instance = sprite.newSprite(set);  
  
 instance.x = display.contentWidth/70   
physics.addBody(instance, {bounce = 0.2, friction = .1, radius = 30})  
instance.name = "sprite"  
  
 instance:play();  
 transition.to( instance, { time=15000, x = 1100 } )  

Play Button:

local playbutton = display.newImage( "play.png" )  
playbutton.x = display.stageWidth / 1.195  
playbutton.y = display.stageHeight / 4.7  
  
 playbutton.active = false  
local function buttonTouched(e) if e.phase == "began" then  
e.target.alpha = .5  
end if e.phase == "ended" then  
e.target.alpha = 1 print("Replay button was touched.")  
 end  
end  
  
playbutton:addEventListener( "touch", buttonTouched )  
  
   
   
playbutton:addEventListener('tap', playbutton)  

Right now the Play button doesn’t do anything and when I relaunch the simulator the sprite automatically starts its course across the screen. I’d like to be able to launch the simulator and click the play button and then have the sprite start its course.

Any help would be appreciated.
Thanks,
Andrew [import]uid: 72845 topic_id: 12576 reply_id: 312576[/import]

Move the play and the transition into the play button; you just have them out there on their own, so they are called immediately. [import]uid: 52491 topic_id: 12576 reply_id: 46082[/import]

Could you show me how please? I’ve tried a few things including:

local playbutton = display.newImage( "play.png" )  
playbutton.x = display.stageWidth / 1.531  
playbutton.y = display.stageHeight / 4.7  
  
 playbutton.active = false  
local function buttonTouched(e) if e.phase == "began" then  
e.target.alpha = .5  
end if e.phase == "ended" then  
e.target.alpha = 1 print("Replay button was touched.")  
 end  
end  
 local instance = sprite.newSprite(set);  
  
 instance.x = display.contentWidth/70   
physics.addBody(instance, {bounce = 0.2, friction = .1, radius = 30})  
instance.name = "gnomie"  
  
 instance:play();  
 transition.to( instance, { time=15000, x = 1100 } )   
  
playbutton:addEventListener( "touch", buttonTouched )  
  
   
   
playbutton:addEventListener('tap', playbutton)  

Thanks again Peach,
Andrew [import]uid: 72845 topic_id: 12576 reply_id: 46102[/import]

Something like this;

[lua]local playbutton = display.newImage( “play.png” )
playbutton.x = display.stageWidth / 1.531
playbutton.y = display.stageHeight / 4.7

local instance = sprite.newSprite(set);
instance.x = display.contentWidth/70
physics.addBody(instance, {bounce = 0.2, friction = .1, radius = 30})
instance.name = “gnomie”

playbutton.active = false
local function buttonTouched(e) if e.phase == “began” then
e.target.alpha = .5
end if e.phase == “ended” then
instance:play();
transition.to( instance, { time=15000, x = 1100 } )
e.target.alpha = 1 print(“Replay button was touched.”)
end
end

playbutton:addEventListener( “touch”, buttonTouched )

playbutton:addEventListener(‘tap’, playbutton)[/lua]

See how it’s inside the button? You didn’t have it in a function at all. [import]uid: 52491 topic_id: 12576 reply_id: 46218[/import]

Ohhhh, thank you so much. You’re definitely getting in honorable mention in my game :slight_smile: [import]uid: 72845 topic_id: 12576 reply_id: 46232[/import]

Is their a way to make it stop after it starts playing?

I’ve tried a few things but I end up making another sprite so I have 2 on the screen instead of 1 [import]uid: 72845 topic_id: 12576 reply_id: 46241[/import]

Hey again,

Yes, there is; http://developer.anscamobile.com/reference/index/spriteinstancepause

Peach :slight_smile: [import]uid: 52491 topic_id: 12576 reply_id: 46392[/import]

I figured out how to “stop” the sprite as far as animation but its still transitioning to the other side of the screen. any idea how to stop both?

--Pause button  
  
local pausebutton = display.newImage( "pausebutton.png" )  
pausebutton.x = display.stageWidth / 1.29  
pausebutton.y = display.stageHeight / 4.7  
  
 pausebutton.active = false  
local function buttonTouched(e) if e.phase == "began" then  
e.target.alpha = .5  
end if e.phase == "ended" then  
instance:pause();  
e.target.alpha = 1 print("Replay button was touched.")  
 end  
end  
  
pausebutton:addEventListener( "touch", buttonTouched )  
  
   
   
pausebutton:addEventListener('tap', pausebutton)  
  
--end pause button  

the instance:pause(); is stopping the animation of the sprite but not the transition

 transition.to( instance, { time=15000, x = 1100 } )  

I’ve already made my special thanks screen and I added ur name if thats ok : ) [import]uid: 72845 topic_id: 12576 reply_id: 46912[/import]

Heh, that’s nice of you - thanks :slight_smile:

Now - it isn’t stopping the transition because the transition and the sprite are not the same thing.

Name the transition by changing that line to;
[lua]trans1 = transition.to( instance, { time=15000, x = 1100 } )[/lua]

Then when you want to stop the transition just put in this line;
[lua]transition.cancel(trans1)[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 12576 reply_id: 46931[/import]