Newbie - Flinging ice across the screen in top down

Hello,

I’m trying to get an idea of how a user could touch a block of ice on the screen and then fling it across.  The issue is that if I use a standard set of drag commands, once the user let’s go, the ice just stops.  I need it to

move based on that user’s force and then slide for a little bit.  Does anyone have any ideas?  

Hello,

I’m trying to get an idea of how a user could touch a block of ice on the screen and then fling it across.  The issue is that if I use a standard set of drag commands, once the user let’s go, the ice just stops.  I need it to

move based on that user’s force and then slide for a little bit.  Does anyone have any ideas?  

Current Code:

–basic setup

–Turn on physics

    

    local physics = require “physics”

    

    physics.start()

    physics.setGravity(0,0)

    

– Prevent the app from becoming suspended

system.setIdleTimer(false);

    

–Turn on sprites

    

    local sprite = require(“sprite”)

–Hide Status Bar

    

    display.setStatusBar(display.HiddenStatusBar)

    local centerX = display.contentCenterX

    local centerY = display.contentCenterY

    --or

    _W = display.contentWidth;

    _H = display.contentHeight;

–end basic setup

–setup forward references

–end forward references

–mpc graphics

    local ice = display.newImage(“2d/ice.png”)

    ice.xScale = .5

    ice.yScale = .4

    ice.y = centerY

    

    physics.addBody(ice,{ density = 1.0, friction = 0.3, bounce = 0.2 } )

    

    

–tubes

    

    local tube = display.newImage(“2d/tube1.png”)

    tube.alpha = .5

    tube.xScale = .5

    tube.yScale = .5

    tube.x = centerX - 200

    tube.y = centerY

    

    local tube2 = display.newImage(“2d/tube1.png”)

    tube2.alpha = .5

    tube2.xScale = .5

    tube2.yScale = .5

    tube2.x = centerX - 20

    tube2.y = centerY

    

    local tube3 = display.newImage(“2d/tubecrossroad.png”)

    tube3.alpha = .5

    tube3.xScale = .5

    tube3.yScale = .5

    tube3.x = centerX + 160

    tube3.y = centerY

    

    local tube4 = display.newImage(“2d/tubetwist.png”)

    tube4.alpha = .5

    tube4.xScale = .5

    tube4.yScale = .5

    tube4.x = centerX + 117

    tube4.y = centerY - 180

    

    

–wall graphics

    local leftWall = display.newRect(0, 0, 10 ,display.contentHeight)

    local rightWall = display.newRect(470, 0, 10 ,display.contentHeight)

    --push Mechanism

local function onTouch( event )

– Get the target object

local target = event.target

– Check the phase of the event. Here the drag begins

if event.phase == “began” then – When the event begins

local parent = target.parent – get the parent object

parent:insert( target ) – move the drag object to the top

– The dragging object needs to keep focus

display.getCurrentStage():setFocus( target )

target.isFocus = true – Mark this object as having focus

– Get the offset from center of the object to the location

– of the event

target.offsetX = event.x - target.x

target.offsetY = event.y - target.y

– This phase handles moving the object

elseif target.isFocus and event.phase == “moved” then

target.x = event.x - target.offsetX – Position the object

target.y = event.y - target.offsetY – using the offsets

– This phase ends the drag

elseif event.phase == “ended” or event.phase == “cancelled” then

display.getCurrentStage():setFocus( nil ) – reset focus

target.isFocus = false – mark this object as no longer having focus

end

end

ice:addEventListener(“touch”, onTouch)

Physics would be the easiest way to assign force. I haven’t done much with physics, but you can apply a force to an object, and a friction on the surface as well. 

Simple to do a few tests. Just use a touch command to start with. When you touch the ice block, just give it a random direction to get used to using the physics stuff, then add speed based on the dragging.

I’ll try!

Hello,

I’m trying to get an idea of how a user could touch a block of ice on the screen and then fling it across.  The issue is that if I use a standard set of drag commands, once the user let’s go, the ice just stops.  I need it to

move based on that user’s force and then slide for a little bit.  Does anyone have any ideas?  

Current Code:

–basic setup

–Turn on physics

    

    local physics = require “physics”

    

    physics.start()

    physics.setGravity(0,0)

    

– Prevent the app from becoming suspended

system.setIdleTimer(false);

    

–Turn on sprites

    

    local sprite = require(“sprite”)

–Hide Status Bar

    

    display.setStatusBar(display.HiddenStatusBar)

    local centerX = display.contentCenterX

    local centerY = display.contentCenterY

    --or

    _W = display.contentWidth;

    _H = display.contentHeight;

–end basic setup

–setup forward references

–end forward references

–mpc graphics

    local ice = display.newImage(“2d/ice.png”)

    ice.xScale = .5

    ice.yScale = .4

    ice.y = centerY

    

    physics.addBody(ice,{ density = 1.0, friction = 0.3, bounce = 0.2 } )

    

    

–tubes

    

    local tube = display.newImage(“2d/tube1.png”)

    tube.alpha = .5

    tube.xScale = .5

    tube.yScale = .5

    tube.x = centerX - 200

    tube.y = centerY

    

    local tube2 = display.newImage(“2d/tube1.png”)

    tube2.alpha = .5

    tube2.xScale = .5

    tube2.yScale = .5

    tube2.x = centerX - 20

    tube2.y = centerY

    

    local tube3 = display.newImage(“2d/tubecrossroad.png”)

    tube3.alpha = .5

    tube3.xScale = .5

    tube3.yScale = .5

    tube3.x = centerX + 160

    tube3.y = centerY

    

    local tube4 = display.newImage(“2d/tubetwist.png”)

    tube4.alpha = .5

    tube4.xScale = .5

    tube4.yScale = .5

    tube4.x = centerX + 117

    tube4.y = centerY - 180

    

    

–wall graphics

    local leftWall = display.newRect(0, 0, 10 ,display.contentHeight)

    local rightWall = display.newRect(470, 0, 10 ,display.contentHeight)

    --push Mechanism

local function onTouch( event )

– Get the target object

local target = event.target

– Check the phase of the event. Here the drag begins

if event.phase == “began” then – When the event begins

local parent = target.parent – get the parent object

parent:insert( target ) – move the drag object to the top

– The dragging object needs to keep focus

display.getCurrentStage():setFocus( target )

target.isFocus = true – Mark this object as having focus

– Get the offset from center of the object to the location

– of the event

target.offsetX = event.x - target.x

target.offsetY = event.y - target.y

– This phase handles moving the object

elseif target.isFocus and event.phase == “moved” then

target.x = event.x - target.offsetX – Position the object

target.y = event.y - target.offsetY – using the offsets

– This phase ends the drag

elseif event.phase == “ended” or event.phase == “cancelled” then

display.getCurrentStage():setFocus( nil ) – reset focus

target.isFocus = false – mark this object as no longer having focus

end

end

ice:addEventListener(“touch”, onTouch)

Physics would be the easiest way to assign force. I haven’t done much with physics, but you can apply a force to an object, and a friction on the surface as well. 

Simple to do a few tests. Just use a touch command to start with. When you touch the ice block, just give it a random direction to get used to using the physics stuff, then add speed based on the dragging.

I’ll try!