Touch Event

I am having a weird issue on a touch event. I want the player character to go where the user touches the screen. He does this but if the player clicks on the ninja and moves him fast in a direction it completely pushes him off the screen. I just want the ninja to go to the location clicked on. Here is my code for this. Any help as always will be much appreciated. swimming is the name of the ninja sprite.

[code]

local function movePlayer( event )

if event.phase ~= ended and gameIsActive then
transition.to( swimming, { time=1500, x = event.x, y = event.y } )
elseif swimming.y > 100 then swimming.y = 100
swimming.velocity = 0
print(swimming.y)
end

–print (“moved player”)

end [import]uid: 49863 topic_id: 20957 reply_id: 320957[/import]

Oh man your using transitions…Try this.
[lua]local function movePlayer( event )
—somewhere up in top
if swimming.movePlayerTran then transition.cancel(swimming.movePlayerTran) end
if event.phase ~= ended and gameIsActive then
swimming.movePlayerTran = transition.to( swimming, { time=1500, x = event.x, y = event.y } )
elseif swimming.y > 100 then swimming.y = 100
swimming.velocity = 0
print(swimming.y)
end

–print (“moved player”)

end[/lua]

That way it will stop the transition and add a new one to the correct position. Its always best to cancel transitions and timers, otherwise they build up on the queue(staff correct me if I am wrong) and theres no telling what it will do. [import]uid: 54716 topic_id: 20957 reply_id: 82763[/import]

I tried that and the ninja still flies off the screen if you hold on to the click and swing him to the side. I tried putting up invisible walls but he still goes through them. Here is the code for the whole thing.

[code]
module(…, package.seeall)

–====================================================================–
– SCENE: Fishbowl Game
–====================================================================–

–[[

  • Version: 0.1
  • Made by Ninja Carnival Team @ 2011

******************

  • INFORMATION
    ******************

  • Fishbowl game

–]]

new = function ( params )

– Imports
local ui = require ( “ui” )

– Groups
local localGroup = display.newGroup()
local bgGroup = display.newGroup()
local scoreLayer = display.newGroup()
– Declare Variables
local gameIsActive = true
local toRemove = {}
local score = 0
local halfPlayerWidth
local toRemoveFish = {}
local rotDirection = 1

– start physics
local physics = require(“physics”)
local sprite = require(“sprite”)
physics.start()
physics.setGravity(0,0 )

– Enemy graphics held in memory
local textureCache = {}
textureCache[1] = display.newImage(“images/Lvl_2/fish.png”)
textureCache[1].isVisible = false
local halfEnemyWidth = textureCache[1].contentWidth * .5

– Pre-loads our sounds
swimming = audio.loadSound(“audio/Lvl_2/swimmingfinal.wav”)
music = audio.loadStream(“audio/Lvl_2/LevelTwoSongFinal.wav”)

– Display Objects
local contentWidth = display.contentWidth

local image1 = display.newImage(“images/Lvl_2/Level_2_BG_1.png”,0,0)
local image3 = display.newImage(“images/Lvl_2/Level_2_BG_3.png”,480,0)
local image4 = display.newImage(“images/Lvl_2/Level_2_BG_4.png”,960,0)
local image5 = display.newImage(“images/Lvl_2/Level_2_BG_5.png”,1440,0)
local image6 = display.newImage(“images/Lvl_2/Level_2_BG_6.png”,1920,0)
local image7 = display.newImage(“images/Lvl_2/Level_2_BG_7.png”,2400,0)
local image8 = display.newImage(“images/Lvl_2/Level_2_BG_8.png”,2880,0)
local image9 = display.newImage(“images/Lvl_2/Level_2_BG_9.png”,3360,0)
local image10 = display.newImage(“images/Lvl_2/Level_2_BG_10.png”,3840,0)

transition.to(image1, { time= 15000, delay = 1500, x= -(image1.width * 0.5)} )
transition.to(image3, { time= 30000, delay = 1500, x= -(image3.width * 0.5) } )
transition.to(image4, { time= 40000, delay = 1500, x= -(image4.width * 0.5) } )
transition.to(image5, { time= 50000, delay = 2500, x= -(image5.width * 0.5) } )
transition.to(image6, { time= 60000, delay = 1500, x= -(image6.width * 0.5) } )
transition.to(image7, { time= 70000, delay = 1500, x= -(image7.width * 0.5) } )
transition.to(image8, { time= 80000, delay = 1500, x= -(image8.width * 0.5) } )
transition.to(image9, { time= 90000, delay = 1500, x= -(image9.width * 0.5) } )
transition.to(image10, { time= 90000, delay = 1500, x= (image10.width) * 0.5} )

scoreText = display.newText(score, 0, 0, native.systemFont, 19)
scoreText:setTextColor(0,0,155)
scoreText.x = 100
scoreImage = display.newImage(“images/Lvl_2/fish_counter_clear.png”)

– BUTTONS

– Functions
local function onCollision(self, event)
if self.name == “swimming” and event.other.name == “fish” and gameIsActive then
score = score + 1
scoreText.text = score
–get a sound effect for this event
table.insert(toRemove, event.other)

elseif self.name == “swimming” and event.other.name == “moving” and gameIsActive then
gameEnd()

elseif self.name == “swimming” and event.other.name == “urchin” and gameIsActive then
gameEnd()
end

end

local function spawnUrchin( event )
local urchin = display.newImage(“images/Lvl_2/urchin_final_56.png”)
urchin.name = “urchin”
physics.addBody(urchin, “kinematic”, {density=1.0, friction=0.5, bounce=0.0, radius= 30})
urchin.y = 320
urchin.x = math.random(50,480)
transition.to(urchin, {time = 20000, y = -320})
print (urchin.y)
return urchin
end

local function spawnFish( event )
local fish = display.newImage(“images/Lvl_2/fish.png”,50,60)
fish.name = “fish”
physics.addBody(fish, “kinematic”, {density=1.0, friction=0.5, bounce=0.0, radius= 30})
fish.x = math.random(480, 480)
fish.y = math.random(50, 320)
transition.to(fish, {time= math.random(2000,4000), x=-250})

return fish
end

function newSwimmingSprite()

swimming = sprite.newSprite(spriteSet2)
swimming:prepare(“default”)
swimming.isHitTestable = false
print (“gotit”)
return swimming
end

function newMovingSquid()

moving = sprite.newSprite(spriteSet3)
moving:prepare(“default”)
moving.isHitTestable = false
print (“gotit”)
return moving
end

local function movePlayer( event )
—somewhere up in top
if swimming.movePlayerTran then
transition.cancel(swimming.movePlayerTran)
end
if event.phase ~= ended and gameIsActive then
swimming.movePlayerTran = transition.to( swimming, { time=1500, x = event.x, y = event.y } )
elseif swimming.y > 100 then swimming.y = 100
swimming.velocity = 0
print(swimming.y)
end

–print (“moved player”)

end

local function spawnPlayer( event )

swimming = newSwimmingSprite()
swimming.name = “swimming”

physics.addBody(swimming, “dynamic”, {density=1.0, friction=0.5, bounce=0.0, radius= 15})
swimming.isSensor = false
swimming.collision = onCollision
swimming:addEventListener(“collision”, swimming)
Runtime:addEventListener(“touch”, movePlayer)
swimming:play()

print (“spawnedPlayer”)
end

local function spawnSquid( event )—working here

moving = newMovingSquid()
moving.name = “moving”

physics.addBody(moving, “kinematic”, {density =1.0, friction = 0.5, bounce = 0.0, radius = 50})
moving:play()

print(“spawned squid”)

end

local function squidPosition (event)

moving.y = moving.y + rotDirection
if moving.y > 320 then
rotDirection = -1
elseif moving.y < 75 then
rotDirection = 1
end

end

function coordUpdate( event )

if swimming.x > 460 then swimming.x = 460
print(swimming.y)
end

end

function gameEnd( event )

gameIsActive = false
swimming:pause()
end

– PARAMETERS

– Listener

– INITIALIZE
local initVars = function ()
– Inserts

bgGroup:insert(image1)
bgGroup:insert(image3)
bgGroup:insert(image4)
bgGroup:insert(image5)
bgGroup:insert(image6)
bgGroup:insert(image7)
bgGroup:insert(image8)
bgGroup:insert(image9)
bgGroup:insert(image10)

scoreLayer:insert(scoreText)
– Positions

– Colors

– Listeners

swimming = sprite.newSpriteSheet( “swim_sheet.png”, 64, 31 )
spriteSet2 = sprite.newSpriteSet(swimming, 1, 4)
sprite.add( spriteSet2, “default”, 1, 4, 1000, 0 )
spawnPlayer()
swimming.x = display.contentWidth * 0.5
swimming.y = display.contentHeight * 0.5

moving = sprite.newSpriteSheet( “oct_spr_sheet.png”, 80, 78 )
spriteSet3 = sprite.newSpriteSet(moving, 1, 7)
sprite.add(spriteSet3, “default”, 1, 7, 1000, 0)
spawnSquid()
moving.x = display.contentWidth * 0.05
moving.y = display.contentHeight * 0.5

local borderLeft = display.newRect( 0, 0, 10, display.contentHeight )
borderLeft:setFillColor( 1, 1, 0) – make invisible
physics.addBody( borderLeft, “static”,{density =10.0, friction = 10.5, bounce = 3.0} )
borderLeft.isSensor = false

local borderTop = display.newRect( 0, 0, display.contentWidth, 10 )
borderTop:setFillColor( 1, 1, 0) – make invisible
physics.addBody( borderTop, “static”,{density =10.0, friction = 10.5, bounce = 3.0})
borderTop.isSensor = false

local borderRight = display.newRect( display.contentWidth - 10, 0, 10, display.contentHeight )
borderRight:setFillColor( 1, 1, 0) – make invisible
physics.addBody( borderRight, “static”,{density =10.0, friction = 10.5, bounce = 3.0})
borderRight.isSensor = false

local borderBottom = display.newRect( 0,display.contentHeight - 10, display.contentWidth, 10 )
borderBottom:setFillColor( 1, 1, 0) – make invisible
physics.addBody( borderBottom, “static”,{density =10.0, friction = 10.5, bounce = 3.0})
borderBottom.isSensor = false

end

– Initiate variables
initVars()

– Update
local update = function ( event )
local timeLastEnemy = 0
if gameIsActive then
for i = #toRemove, 1, -1 do
toRemove[i].parent:remove(toRemove[i])
toRemove[i] = nil
end
for i = #toRemoveFish, 1, -1 do
remove(toRemoveFish[i])
toRemoveFish[i] = nil
end

end

end

–audio.play(music, {loops = -1})
timer.performWithDelay(math.random(500, 1000), spawnFish, 0)
timer.performWithDelay(2500, spawnUrchin, 0)
Runtime:addEventListener(“enterFrame”, update)
Runtime:addEventListener(“enterFrame”, coordUpdate)
Runtime:addEventListener(“enterFrame”, squidPosition)
– MUST return a display.newGroup()
return localGroup

end [import]uid: 49863 topic_id: 20957 reply_id: 82768[/import]

So is this just an impossible issue to fix? [import]uid: 49863 topic_id: 20957 reply_id: 83439[/import]