Corona: code that can allow me to drag an object

Asked By 40 points N/A Posted on -
qa-featured

Hey all!

I am an extreme beginner and just started using Corona. So far I have learned how to put a background and image up and I'm thinking the next step is interacting with my little fish that's on the screen. But I just can't figure out how to do it.

I've read the docs and searched the forums but I believe it's just my ignorance, of this getting in the way.

So I would really appreciate some help! I can give you my code so far and if its not too much trouble, would you be able to show me what the code looks like so I can drag this fish around the screen?

Thank you for the help!

background = display.newImage ( "fishtank.png" )

myImage = display.newImage( "fish.png", 20, 100 )

This is all I have.

SHARE
Best Answer by Peter franklin
Answered By 0 points N/A #95545

Corona: code that can allow me to drag an object

qa-featured

Hi,

You should always try referring to the examples provided by Corona. You can always get ample of guidance from here itself.

Go to the Drag Platform example and you will find out what you need. There you will find dragUI Helpers class.

And the other thing, you were asking is related to Corona Software Development Kit. So you must refer the SDK Guide because you will encounter these kind of problems in future.

Best Answer
Best Answer
Answered By 0 points N/A #95547

Corona: code that can allow me to drag an object

qa-featured

At the beginning of the user's touch. You have to store the current location of the object.

During the move phase. Subtract the stat location.

And also add also store the location of the object. In the end you can also change the location to the newly calculated one.

-- create object
local myObject = display.newRect( 0, 0, 100, 100 )
myObject:setFillColor( 255 )

-- touch listener function
function myObject:touch( event )
    if event.phase == "began" then
	
        self.markX = self.x    -- store x location of object
        self.markY = self.y    -- store y location of object
	
    elseif event.phase == "moved" then
	
        local x = (event.x - event.xStart) + self.markX
        local y = (event.y - event.yStart) + self.markY
        
        self.x, self.y = x, y    -- move object based on calculations above
    end
    
    return true
end

-- make 'myObject' listen for touch events
myObject:addEventListener( "touch", myObject )

This script will allow you to drag an object. I hope this helped

Regards,

Peter Franklin

Answered By 0 points N/A #95549

Corona: code that can allow me to drag an object

qa-featured

You must know the logic behind how an object is dragged? 

I have divided it in two phase – began and move.



The Logic is –

1. At the beginning (i.e. “began” phase event), firstly, you must store the current location of the object.

2. In the second phase i.e. The “move” phase, you shall subtract the start location of the event

See below for coding) from the event’s current location (see below for coding)

3. You shall add the stored location of the object as well.

4. Eventually, you need to change the current location of the object to the new created location which will actually move the object.

Please find the detailed coding here –

— Create object

local myObject = display.newRect( 0, 0, 100, 100 )

myObject:setFillColor( 255 )

— touch listener function

function myObject:touch( event )

    if event.phase == "began" then

        self.markX = self.x    — store x location of object

        self.markY = self.y    — store y location of object

    elseif event.phase == "moved" then

        local x = (event.x – event.xStart) + self.markX

        local y = (event.y – event.yStart) + self.markY

        self.x, self.y = x, y    — move object based on calculations above

    end

    return true

    end

— make 'myObject' listen for touch events

myObject:addEventListener( "touch", myObject )

Answered By 0 points N/A #95550

Corona: code that can allow me to drag an object

qa-featured

Hi,

I’ll give a simple script that will move your fish, where the user (you) will touch the screen.

For this I’ll use your image.  

— your fish image

local image = display.newImageRect( "fish.png", 20, 100 )

image:translate( halfW, halfH )

— Mask

local mask = graphics.newMask( "fish.png" )

image:setMask( mask )

function onTouch( event )

    local t = event.target

    local phase = event.phase

    if "moved" == phase then

        t.maskX = event.x

        t.maskY = event.y

    end

end

image:addEventListener( "touch", onTouch )

Have fun,

Aabel

Login/Register to Answer

Related Questions