Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Move a statict object help
Started by dodi_games Nov 20 2019 12:41 PM

9 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 402 posts
  • Corona SDK

Hi I need to use a static object in my game and I need to make a left and right continues movement with two button.  But my object only move once. Help

 

here is the code of the left button:

local function leftBtnTouch( event )


 local function moveLeft()
  box.x = boxX- 10       --where boxX is the inicial position of the box in scene:create()
 end


 if event.phase == "began" then
   print( "left button touch" )
   moveBoxToLeft = timer.performWithDelay(100, moveLeft(), -1)

 elseif event.phase == "ended" then
  if moveBoxToLeft ~= nil then
   timer.cancel( moveBoxToLeft )
   moveBoxToLeft = nil
  end
  
 end
 return true
end

I set -1 timer interactions to repeat till "ended" phase come but the box only move once.  To give a better example, I am trying to make a game where there is a box below that picks up the objects that fall from above and I need to move it from left to right to catch the objects. The box needs to be static, is the only way I can do my idea using the physics engine. I would not like the player to make multiple taps to move the box, rather I look for a continuous movement that stops at the "ended" phase.

 

Thanks in advance DoDi



[TOPIC: post.html]
#2

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 663 posts
  • Corona SDK

If I understand your problem correctly you want to achieve continuous movement?

box.x = boxX-10 means the object is moving to the same place every time moveLeft() is called.

 

Instead try

box.x = box.x -10



[TOPIC: post.html]
#3

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 402 posts
  • Corona SDK

Thanks for your quick response @sporkfin

It moves but not continuously. What I want is when the user leaves the button pressed, the box keeps moving and when the user releases the button, the movement stops.



[TOPIC: post.html]
#4

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 663 posts
  • Corona SDK

How about dropping the timer and the "began" phase and having the moveLeft() function trigger continuously when the button is touched?  You can then set the rate of movement inside moveLeft() and the action will stop when the button is released.


  • dodi_games likes this

[TOPIC: post.html]
#5

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 402 posts
  • Corona SDK

@sporkfin

Could you show me correcting the code I have? I try to understand what you explain to me and if the language does not betray me, I consider it the same thing I am doing. Sorry but I'm not seeing what you want to explain to me. I really appreciate your help.

[TOPIC: post.html]
#6

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 663 posts
  • Corona SDK

local movementSpeed = 2 -- speed for moveLeft()
 
local function moveLeft()
  box.x = box.x - movementSpeed -- box moves -2 pixels on the x-axis every frame
end
 
local function leftBtnTouch( event )
  moveLeft() -- while the left button is touched, moveLeft() is called every frame
  return true
end
 
leftBtn:addEventListener( "touch", leftBtnTouch ) -- add a "touch" listener - NOT "tap"

  • dodi_games likes this

[TOPIC: post.html]
#7

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 402 posts
  • Corona SDK

 

local function leftBtnTouch( event )
  moveLeft() -- while the left button is touched, moveLeft() is called every frame
  return true
end
 

 

with this code the box only move 2px when the button is pressed and 2px when button release  :wacko:

 

I don't know how to make the timer interactions works well within the function



[TOPIC: post.html]
#8

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 663 posts
  • Corona SDK

Yes and ooops!  That's what should happen, of course. 

 

Use Runtime listeners instead of timers

local movementSpeed = 2 -- speed for moveLeft()
 
local function moveLeft()
  tubey.x = tubey.x - movementSpeed -- box moves -2 pixels on the x-axis every frame
end
 
local function leftBtnTouch( event )
  if event.phase == 'began' then
    Runtime:addEventListener('enterFrame', moveLeft)
  elseif event.phase == 'ended' or event.phase == 'cancelled' then
    Runtime:removeEventListener('enterFrame', moveLeft)
  end
  return true
end
 
leftBtn:addEventListener( "touch", leftBtnTouch ) -- add a "touch" listener - NOT "tap"

  • dodi_games likes this

[TOPIC: post.html]
#9

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 663 posts
  • Corona SDK

Of course, "tubey" was my test object and should be replaced with "box" in your example.



[TOPIC: post.html]
#10

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 402 posts
  • Corona SDK

removing () makes the timer work properly. I missed that one. Thanks again! I will try the "runtime" it looks simpler than the timers.




[topic_controls]
[/topic_controls]