Hi,
I just started working on my first game. In this game characters are moving randomly on the screen and you have to kill them by touch event, problem I am facing in this game is that when I touch the screen character moves but if I release the touch event the character would not moving. how to overcame this problem.
Can you post some code? Don’t forget to put it in side of [lua] and [/lua] tags (without the space between the [ and the “l”)
[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()
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:prepare(“right”)
character:play()
transition.to (character, {time=5000, x = 465, y = 16})
elseif event.phase == “ended” then
character.ishit=true
character.isbullet=true
character.bodyType = “dynamic”
character:prepare(“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, 1 )
drawBackground()
createchar()
Runtime:addEventListener( “touch”, onScreenTouch )
end
gameInit()[/lua]
that’s the code, I have written so far for the movement of character
When you say “Moving” do you mean the sprite is animating? I don’t see any code that moves the character until you touch it when you transition it and when you let up you try to play another animation and move the character to a new location.
yes, in this code moving the sprite means animating it.
But what I want in this game is that the character moves freely any where on the screen and when it collide with the screen boundaries, it bounces back to the other direction. And want to do this happen in single touch.
unfortunately do not know how to do this
It has something to do with preparing a new instance on an existing one, but sprites are not my specialty. I’ll get someone to peek in on this for you.
ok
And I will try my best to complete this…!
Thanks
Hi @furqan.2712,
The first thing I’d suggest is porting your code to the current sprite method(s). The “sprite” library you’re using was deprecated quite some time ago. It only remains around for backwards compatibility, and it won’t get any support going forward.
Sprites are now handled as part of the “display” library, and I recommend you convert over to that. Follows is a tutorial on the subject. If you can convert over and then repost your code again (if you have problems), we can take a look then.
http://www.coronalabs.com/blog/2012/10/02/animated-sprites-and-methods/
Thanks,
Brent Sorrentino
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]
Hi @furqan.2712,
OK, great… let’s solve the main issue then.
I suggest that you set the transition as a property of character itself (it’s easy to reference that way). In both the “began” and “ended” event parts, check that the transition exists (for the first instance), cancel it, then re-start a new transition as the same property. Like this:
[lua]
if ( character.moveTrans ) then transition.cancel( character.moveTrans ) end
character.moveTrans = transition.to (character, { time=5000, x = 13, y = 16} )
[/lua]
This will stop any running transition before starting the new one.
Is this what you need? Maybe I didn’t understand exactly how this game should work…
Brent
Thanks Brent Sorrentino
here’s a picture of game that was built in java, I am trying to make it using corona SDK.
And I simply used delay function in my code and it solve my problem of transition from right to left.
[lua]timer.performWithDelay( 5000, function() left(); end, 1 )[/lua]
but as you see in picture the sprites are moving on the screen randomly. Is there any function in lua for doing this?
Ah, I see… so you want a lot of “random characters” moving around the screen, and when they touch a “wall”, they turn around (bounce off) and start moving elsewhere?
yes, that’s the main logic of my game…!
Can you post some code? Don’t forget to put it in side of [lua] and [/lua] tags (without the space between the [ and the “l”)
[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()
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:prepare(“right”)
character:play()
transition.to (character, {time=5000, x = 465, y = 16})
elseif event.phase == “ended” then
character.ishit=true
character.isbullet=true
character.bodyType = “dynamic”
character:prepare(“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, 1 )
drawBackground()
createchar()
Runtime:addEventListener( “touch”, onScreenTouch )
end
gameInit()[/lua]
that’s the code, I have written so far for the movement of character
When you say “Moving” do you mean the sprite is animating? I don’t see any code that moves the character until you touch it when you transition it and when you let up you try to play another animation and move the character to a new location.
yes, in this code moving the sprite means animating it.
But what I want in this game is that the character moves freely any where on the screen and when it collide with the screen boundaries, it bounces back to the other direction. And want to do this happen in single touch.
unfortunately do not know how to do this
It has something to do with preparing a new instance on an existing one, but sprites are not my specialty. I’ll get someone to peek in on this for you.
ok
And I will try my best to complete this…!
Thanks
Hi @furqan.2712,
The first thing I’d suggest is porting your code to the current sprite method(s). The “sprite” library you’re using was deprecated quite some time ago. It only remains around for backwards compatibility, and it won’t get any support going forward.
Sprites are now handled as part of the “display” library, and I recommend you convert over to that. Follows is a tutorial on the subject. If you can convert over and then repost your code again (if you have problems), we can take a look then.
http://www.coronalabs.com/blog/2012/10/02/animated-sprites-and-methods/
Thanks,
Brent Sorrentino