Hi Brent Sorrentino,
Thanks for your information about the “display” library and tutorial, I read the tutorial and do some editing in the code according to that,…
here is the editd code, And now I have the same problem I have mentioned above that when I touch the screen the character animates in right direction and when leaved the touch event it animates in left direction but I want to do this in single touch event.
[lua]-- Hide Status Bar
display.setStatusBar( display.HiddenStatusBar )
– Sprite
–local sprite = require(“sprite”)
– Physics
local physics = require (“physics”)
– Variables
local background
local character
----------|Backgroung function|--------
local drawBackground = function()
background = display.newImage( “bg.png” )
local leftWall = display.newRect (0, 0, 0, display.contentHeight)
leftWall.name = “wall”
local rightWall = display.newRect (display.contentWidth, 0, 0, display.contentHeight)
rightWall.name = “wall”
local ceiling = display.newRect(0, 0, display.contentWidth, 1 )
ceiling.name = “wall”
local floor = display.newRect(0, display.contentHeight, display.contentWidth, 1 )
floor.name = “wall”
physics.addBody (leftWall, “static”, {bounce = 0.0, friction = 10})
physics.addBody (rightWall, “static”, {bounce = 0.0, friction = 10})
physics.addBody (ceiling, “static”, {bounce = 0.0, friction = 10})
physics.addBody (floor, “static”, {bounce = 0.0, friction = 10})
end
--------|Create Character function|-------
local createchar = function()
--local sheet1 = sprite.newSpriteSheet( “image.png”, 32, 32)
--local instance1 = sprite.newSpriteSet( sheet1, 1, 12 )
--sprite.add( instance1, “front”, 1, 3, 1000, 0 )
--sprite.add( instance1, “left”, 4, 3, 1000, 0 )
--sprite.add( instance1, “right”, 7, 3, 1000, 0 )
--sprite.add( instance1, “back”, 10, 3, 1000, 0 )
--character = sprite.newSprite( instance1 )
--character:prepare(“front”)
--character:play()
local sheetData = { width=32, height=32, numFrames=12, sheetContentWidth=96, sheetContentHeight=128 }
local mySheet = graphics.newImageSheet( “image.png”, sheetData )
local sequenceData = {
{ name = “front”, frames={ 1,2,3 }, time=1000 },
{ name = “left”, frames={ 4,5,6 }, time=1000 },
{ name = “right”, frames={ 7,8,9 }, time=1000 },
{ name = “back”, frames={ 10,11,12 }, time=1000 }
}
character = display.newSprite( mySheet, sequenceData )
character:setSequence( “front” )
character:play()
character.isVisible = false
character.x = 13
character.y = 16
physics.addBody( character, “static”, { density=1.0, bounce=0.0, friction=0.15 } )
end
---------|Touch Screen function|-------
local onScreenTouch = function( event )
character.isVisible = true
if event.phase == “began” then
character.ishit=true
character.isbullet=true
character.bodyType = “dynamic”
character:setSequence( “right” )
character:play()
transition.to (character, {time=5000, x = 465, y = 16})
--elseif “moved” == event.phase then
--character:prepare(“left”)
--character:play()
--transition.to (character, {time=5000, x = 13, y = 16})
elseif event.phase == “ended” then
--local x = event.x
--local y = event.y
--local xForce = (character.x-x) * 4
--local yForce = (character.y-y) * 4
--character:prepare(“left”)
--character:play()
--character.bodyType = “dynamic”
--character:applyForce( xForce, yForce, character.x, character.y )
character:setSequence( “left” )
character:play()
transition.to (character, {time=5000, x = 13, y = 16})
end
end
--------|Main function|------
local gameInit = function()
– PHYSICS
physics.start( true )
physics.setGravity( 0, 0 )
drawBackground()
createchar()
Runtime:addEventListener( “touch”, onScreenTouch )
end
gameInit()[/lua]