Onscreentouch Function

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. :slight_smile:

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…!

Hi @furqan.2712,

I just realized I didn’t respond to this for some time, sorry. Did you ever solve it? Your main logic sounds very similar to the Corona sample app “Fishies”. Have you seen that? It’s located in your local Corona SDK folder here:

Corona SDK > SampleCode > Graphics > Fishies

Check that out and see if it’s what you’re looking for (or similar).

Brent

Hi @furqan.2712,

I just realized I didn’t respond to this for some time, sorry. Did you ever solve it? Your main logic sounds very similar to the Corona sample app “Fishies”. Have you seen that? It’s located in your local Corona SDK folder here:

Corona SDK > SampleCode > Graphics > Fishies

Check that out and see if it’s what you’re looking for (or similar).

Brent

Hi Brent Sorrentino,

Thank you very much and sorry I replied so late, I was  busy in my papers…

here is the link for what I have done so far: http://forums.coronalabs.com/topic/33865-need-help-to-control-speed-sprite-speed/ and also I am facing problem in it so please visit it and I will be very grateful to you for your help so far :slight_smile:

Hi Brent Sorrentino,

Thank you very much and sorry I replied so late, I was  busy in my papers…

here is the link for what I have done so far: http://forums.coronalabs.com/topic/33865-need-help-to-control-speed-sprite-speed/ and also I am facing problem in it so please visit it and I will be very grateful to you for your help so far :slight_smile: