Thanks for the code, but it’s still not working!
I have the code here. And commented it, so you know what im doing. All you need is one image, for the button. Thanks! I think you will notice the problems. All i would like is when you press the button, the ball will have the impulse of speed with the velocity which you have done
I guess it’s just something to fix now. The ball is bouncing a lot, and does not stop after a while, i just want there to be an impulse then returns to normal as in no more speed or velocity. Thanks again.
The code:
[code]
local ui = require(“ui”)
display.setStatusBar( display.HiddenStatusBar )
local physics = require (“physics”)
physics.start(true)
–physics.setDrawMode “hybrid”
physics.setGravity(15, 0)
local ball = display.newCircle( 0, 0, 18)
ball:setFillColor(0, 255, 0)
ball.x = display.contentHeight/7
ball.y = display.contentWidth/7
– This makes the ball drop (dynamic)
local button1Press = function( event )
physics.addBody(ball, { density = 0.2, bounce=0.3, radius = 15, friction=0.5})
end
local drop = ui.newButton{
default = “drop.png”,
over = “drop.png”,
onPress = button1Press,
emboss = true
}
drop.x = display.contentWidth/14
drop.y = display.contentHeight/12
drop.rotation = -90
– The button to add speed to the ball.
–If you try it out you will see it bouncing, or attaching to an objecta bouncing very quickly.
local button2Press = function( event )
local faster = 0.01
local speedX,speedY = ball:getLinearVelocity()
– I think it’s the velocity? That’s the reason i wanted a timer.
function ballSpeed(event)
ball:applyLinearImpulse(
faster * speedX,
faster * speedY,
ball.x,
ball.y
)
end
Runtime:addEventListener(“enterFrame”, ballSpeed)
end
local drop1 = ui.newButton{
default = “drop.png”,
over = “drop.png”,
onPress = button2Press,
emboss = true
}
drop1.x = display.contentWidth/14
drop1.y = display.contentHeight/1.1
drop1.rotation = -90
– Draw a line (swipe on the simulator)
local i = 1
local tempLine
local ractgangle_hit = {}
local prevX , prevY
local function runTouch(event)
if(event.phase==“began”) then
if(tempLine==nil) then
tempLine=display.newLine(event.x, event.y, event.x, event.y)
– Random Colors for line
r = math.random (0, 255)
g = math.random ( 0, 255)
b = math.random (0, 255 )
tempLine:setColor(r,g, b)
prevX = event.x
prevY = event.y
end
elseif(event.phase==“moved”) then
tempLine:append(event.x,event.y-2)
tempLine.width=tempLine.width+0.9
ractgangle_hit[i] = display.newLine(prevX, prevY, event.x, event.y)
ractgangle_hit[i]:setColor(r,g, b)
ractgangle_hit[i].width = 5
– Creates physic joints for line (Turn on draw mode to see the effects)
local Width = ractgangle_hit[i].width * 0.6
local Height = ractgangle_hit[i].height * 0.2
– Physic body for the line shape
local lineShape = {-Width,-Height,Width,-Height,Width,Height,-Width,Height}
physics.addBody(ractgangle_hit[i], “static”, { bounce = -1, density=0.3, friction=0.7, shape = lineShape})
prevX = event.x
prevY = event.y
i = i + 1
elseif(event.phase==“ended”) then
tempLine.parent.remove(tempLine)
tempLine=nil
end
end
Runtime:addEventListener(“touch”, runTouch)
[import]uid: 23689 topic_id: 12621 reply_id: 46511[/import]