I’m trying to slow my player (a draggable object) down so the person playing can’t cheat by swiping it around the screen very quickly (it passes through enemies without registering a collision if dragged fast enough). To implement this, I want to turn the player movement into a rapid series of transitions that fire a set number of milliseconds apart (say, 100 or so). One way I’ve thought of doing this would be with the use of a timer, but I’m not exactly sure where to position this code (inside or outside the drag function), or what variables (prevx,prevy, eventx,eventy) to compare. Can anyone help me with this? My drag function is your standard drag function:
--Player movement functionlocal function onTouch( event ) local player = event.target local phase = event.phase if "began" == phase then -- Make target the top-most object local parent = player.parent parent:insert( player ) display.getCurrentStage():setFocus( player ) player.isFocus = true -- Store initial position player.x0 = event.x - player.x player.y0 = event.y - player.y elseif player.isFocus then if "moved" == phase then player.x = event.x - player.x0 player.y = event.y - player.y0 end end return true end[/code]Thank you,Steven [import]uid: 79394 topic_id: 16837 reply_id: 316837[/import]
You are correct about using a timer function, and there are multiple ways to do it, but something like this will work (pseudocode of course)
local canFire = true
local function fire()
if canFire then
–FIRE
timer.performWithDelay(100, delay, 1)
end
end
local function delay(event)
canFire = true
end
Note: You could set this up in the began phase so force the player to have to keep hitting the button, or the held/moved/whatever that phase is called so that the player can keep firing without letting go of the screen.
Good luck! [import]uid: 36054 topic_id: 16837 reply_id: 63056[/import]
I think something liek this will do the job. But you may need to tweak it to get the expected behaviour.
[lua]local player = display.newCircle(240,240,15)
–Player movement function
local function onTouch( event )
local player = event.target
local phase = event.phase
if “began” == phase then
– Make target the top-most object
local parent = player.parent
parent:insert( player )
display.getCurrentStage():setFocus( player )
player.isFocus = true
– Store initial position
player.x0 = event.x - player.x
player.y0 = event.y - player.y
player.x1 = player.x0
player.y1 = player.y0
elseif player.isFocus then
if “moved” == phase then
if math.abs(player.y - player.y1) > 300 then
display.getCurrentStage():setFocus( nil )
player.isFocus = false
else
player.x = event.x - player.x0
player.y = event.y - player.y0
end
end
end
return true
end
local function checkPosition()
player.x1 = player.x
player.y1 = player.y
end
timer.performWithDelay(100, checkPosition ,-1)
player:addEventListener(“touch”,onTouch)[/lua] [import]uid: 71210 topic_id: 16837 reply_id: 63057[/import]
just a small note of caution…
even if you are after autofire, keep in mind that the games generally run at 30fps or 60fps. which would mean that each frame is about 3 - 6 ms. Your timer running at 100ms fires about 10 times per second. Plus if your function stalls the code, it might eat up more than that little window of 10ms that it has before it fires again.
The way it is handled is then based on time elapsed, the movements, etc are all calculated based on that dT (currTime - lastTime)
thought you might want to know, if you are getting into granularity of ms.
cheers,
?
[import]uid: 3826 topic_id: 16837 reply_id: 63060[/import]
@Jayant, any alternative you would like to suggest ? [import]uid: 71210 topic_id: 16837 reply_id: 63065[/import]
I shall be putting all of these in my book, so I guess a few sleeps and a little wait… before it all comes together.
What would be the point of having info in a book that is already available, even if it from me…
cheers,
?
[import]uid: 3826 topic_id: 16837 reply_id: 63066[/import]
Hi Renjith,
Thanks for getting this out there so quickly!
It definitely is doing what I want it to, and consistently I might add. I really like the way it’s working, but I can’t seem to grasp the concept of what is actually taking place. Do you mind giving me a brief explanation?
One thing I noticed is that if I comment out lines 15 and 16, it doesn’t seem to have any noticeable effect.
Thanks!
Steven
[import]uid: 79394 topic_id: 16837 reply_id: 63074[/import]
line 15 and 16 is not necessary. I just put it to cater for the case if the object is moved before the timer is triggered for the first time.
the code will take the value of player x and y at a particular interval by using a timer. and in our drag code we just check whether the current position and position at which the last timer triggered. by comparing this two we can see how much the object moved after the last triggered. we compare this value with any value we want(in the example its 300). so if after the last trigger of timer if value changed more than 300 we just take the object from the touch focus. so the object no longer moves. as for your game, you are concerned only about movement in y axis am just checking the y values of the object and object position at the last timer trigger.
hope this is clear.
let me know if this is more confusing
[import]uid: 71210 topic_id: 16837 reply_id: 63078[/import]
Not in the slightest, it was very clear. I guess I was just confused because if you comment out lines 15 and 16, I thought line 19 would cause an error because the value of player.y1 hasn’t been declared yet. But now I figure this line doesn’t really call for a value until the end of the code where the eventListener triggers the onTouch event. If I’m mistaken, please correct me!
Either way, I’m very grateful to you for helping me out with this, and for the explanation as well.
Much thanks,
Steven/Eda [import]uid: 79394 topic_id: 16837 reply_id: 63088[/import]
[import]uid: 71210 topic_id: 16837 reply_id: 63089[/import]