Hi,
I’m kind of new to developing and wanted to know how to include a speed booster like in bubble ball when your object hits it and speeds up. Thanks [import]uid: 33363 topic_id: 6333 reply_id: 306333[/import]
Hi,
I’m kind of new to developing and wanted to know how to include a speed booster like in bubble ball when your object hits it and speeds up. Thanks [import]uid: 33363 topic_id: 6333 reply_id: 306333[/import]
http://developer.anscamobile.com/reference/index/bodyapplylinearimpulse
http://developer.anscamobile.com/code/flashs-hittestobject-emulated-using-contentbounds [import]uid: 12108 topic_id: 6333 reply_id: 21933[/import]
I added the code shown to my game and since i’m new at this, i’m having problems too. I have a bomb that is falling and when it hits a button, I want it to accelerate. Here is the code I put in.
local function hitTestObjects(button, bomb)
local left = button.contentBounds.xMin <= bomb.contentBounds.xMin and button.contentBounds.xMax >= bomb.contentBounds.xMin
local right = button.contentBounds.xMin >= bomb.contentBounds.xMin and button.contentBounds.xMin <= bomb.contentBounds.xMax
local up = button.contentBounds.yMin <= bomb.contentBounds.yMin and button.contentBounds.yMax >= bomb.contentBounds.yMin
local down = button.contentBounds.yMin >= bomb.contentBounds.yMin and button.contentBounds.yMin <= bomb.contentBounds.yMax
return (left or right) and (up or down)
end
if hitTestObjects == true then
bomb:applyLinearImpulse( 1000, 0, bomb.x - 10, bomb.y )
[import]uid: 32061 topic_id: 6333 reply_id: 21977[/import]
1. Do I need to add an event listener to the bomb?
yes
2. Did I even build the function correctly?
I don’t know what you mean by “build the function.” Do you mean “use the function”? Because no, you aren’t using it correctly. Refer to the example on the Code Exchange page; call hitTestObjects() with the two objects to check.
So your code would look something like:
local function speedBoost(event)
if hitTestObjects(button, bomb) then
bomb:applyLinearImpulse(1000, 0, bomb.x - 10, bomb.y)
end
end
bomb:addEventListener("enterFrame", speedBoost)
Incidentally, now that I think of it, it may be better to use sensors than hitTestObjects(), since sensors are used with physics while hitTestObjects() is intended for using without physics:
http://developer.anscamobile.com/content/game-edition-physics-bodies#Sensors [import]uid: 12108 topic_id: 6333 reply_id: 22004[/import]
Thanks for the help! I have the speedBoost button not applying physics, so the bomb that is using physics won’t be blocked. [import]uid: 32061 topic_id: 6333 reply_id: 22008[/import]
Have you looked at the link I posted? Sensors are physics bodies that fire off collision events but don’t otherwise affect anything. [import]uid: 12108 topic_id: 6333 reply_id: 22013[/import]
local button = display.newImage("images/button.png")
button.x = 120; button.y = 50
localGroup:insert(button)
local function hitTestObjects(button, bomb)
local left = button.contentBounds.xMin \<= bomb.contentBounds.xMin and button.contentBounds.xMax \>= bomb.contentBounds.xMin
local right = button.contentBounds.xMin \>= bomb.contentBounds.xMin and button.contentBounds.xMin \<= bomb.contentBounds.xMax
local up = button.contentBounds.yMin \<= bomb.contentBounds.yMin and button.contentBounds.yMax \>= bomb.contentBounds.yMin
local down = button.contentBounds.yMin \>= bomb.contentBounds.yMin and button.contentBounds.yMin \<= bomb.contentBounds.yMax
return (left or right) and (up or down)
end
local function speedBoost(event)
if hitTestObjects(button, bomb) then
bomb:applyLinearImpulse(1000, 0, bomb.x - 10, bomb.y)
end
end
imageTable = {}
for i = 1,2 do
table.insert( imageTable, "images/bomb" .. i .. ".png" )
end
for i = 1,9 do
table.insert( imageTable, "images/explode" .. i .. ".png" )
end
local bomb = movieclip.newAnim( imageTable )
bomb.x = 40; bomb.y = -100
physics.addBody( bomb, { density=3.0, friction=0.4, bounce=0.2, radius=19 } )
localGroup:insert(bomb)
bomb:reverse{ startFrame=2, endFrame=1, loop=0 } -- play specified sequence only
-- Create explosion
local function onCollision ( event )
if event.phase == "began" and bomb.y \> 375 then
bomb:play{ startFrame=3, endFrame=11, loop=1, remove=true }
audio.play( bombSound )
bomb:applyForce( 0, 0, bomb.x - 10, bomb.y )
end
end
bomb:addEventListener( "collision", onCollision )
bomb:addEventListener("enterFrame", speedBoost)
This is the code I put in. At first, the game screen went blank. Then I put the code after the button and bomb and the level worked as normal, but without the boost. Does the order in which you add objects and functions matter?
I’m new to coding and just trying to figure this stuff out on the fly. This forum and people who help like you are awesome. [import]uid: 32061 topic_id: 6333 reply_id: 22022[/import]
Hi,
I came across this post and I been trying to make it work
but no luck so far. I have tried couple of changes to
the functions ect… and so far nothing seems to make
my object give the boost to the other object.
I went to all the links Jhocking(Thank You) post above
and I copy the code and paste but i`m not having any luck
it did ran as true when object overlap on terminal but it still
wont give the boost when object falls and hits the other object.
does anyone mind sharing a simple code where a object(obj1) falls
and hits the other object(obj2) and obj2 gives a boost to obj1 so i can get something
going here please thank you very much.
This is what I got so far and when I hit a button ball drops and hits
the right boost button but nothing happens.
– Adding Physics –
local physics = require(“physics”)
physics.start()
– Setting Gravity –
physics.setGravity(0, 5.0)
– Draw Mode Hybrid –
–physics.setDrawMode(“hybrid”)
– Hide Status Bar –
display.setStatusBar(display.HiddenStatusBar)
– Height / Width –
local _H = display.contentHeight/2
local _W = display.contentWidth/2
local bkg = display.newImage(“bkg.png”)
bkg.x = _W
bkg.y = _H
local startbtn = display.newImage(“startbtn.png”)
startbtn.x = 430
startbtn.y = 30
local ball = display.newImage(“ball.png”)
ball.x = 40
ball.y = 30
physics.addBody(ball, “kinematic”, {isSensor=true, density=2.0, bounce=0.1, friction=0, radius=10 } )
local rightboost = display.newImage(“btn.png”)
rightboost.x = 40
rightboost.y = 80
physics.addBody(rightboost, “static” )
function ballDrop(e)
if(e.phase == “ended”)then
ball.bodyType = “dynamic”
end
end
function hitTestObjects(obj1, obj2)
local left = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin
local right = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMin <= obj2.contentBounds.xMax
local up = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin
local down = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMin <= obj2.contentBounds.yMax
return (left or right) and (up or down)
end
local function speedBoost(event)
if hitTestObjects(ball, rightboost) then
ball:applyLinearImpulse(1000, 0)–, ball.x - 10, ball.y)
end
end
ball:addEventListener(“enterFrame”, speedBoost)
rightboost:addEventListener(“touch”, speedBoost)
startbtn:addEventListener(“touch”, ballDrop) [import]uid: 30314 topic_id: 6333 reply_id: 22230[/import]
In your hitTestObject function you still have obj1 and obj2. Don’t you need to change those to the objects you’re trying to collide? [import]uid: 32061 topic_id: 6333 reply_id: 22346[/import]
So this is the best I can come up with. Nothing happens to the bomb when it crosses the path of the button.
I have two functions going on here. First, the bomb should accelerate when it hits the button: it’s not doing that. Second, the bomb should explode. That is working. Is there a problem with the two functions that the explosion is effecting the accelerator? Please help! Thanks!
local button = display.newImage("images/button.png")
button.x = 120; button.y = 50
localGroup:insert(button)
-- BOMB 1 Initialize bomb animation (this time by creating the image table dynamically) --
imageTable = {}
for i = 1,2 do
table.insert( imageTable, "images/bomb" .. i .. ".png" )
end
for i = 1,9 do
table.insert( imageTable, "images/explode" .. i .. ".png" )
end
local bomb = movieclip.newAnim( imageTable )
bomb.x = 40; bomb.y = -100
physics.addBody( bomb, { density=3.0, friction=0.4, bounce=0.2, radius=19 } )
localGroup:insert(bomb)
bomb:reverse{ startFrame=2, endFrame=1, loop=0 } -- play specified sequence only
--Add Accelerator
local function hitTestObjects(button, bomb)
local left = button.contentBounds.xMin \<= bomb.contentBounds.xMin and button.contentBounds.xMax \>= bomb.contentBounds.xMin
local right = button.contentBounds.xMin \>= bomb.contentBounds.xMin and button.contentBounds.xMin \<= bomb.contentBounds.xMax
local up = button.contentBounds.yMin \<= bomb.contentBounds.yMin and button.contentBounds.yMax \>= bomb.contentBounds.yMin
local down = button.contentBounds.yMin \>= bomb.contentBounds.yMin and button.contentBounds.yMin \<= bomb.contentBounds.yMax
return (left or right) and (up or down)
end
local function speedBoost(event)
if hitTestObjects(button, bomb) then
bomb:applyLinearImpulse(1000, 1000, bomb.x, bomb.y)
end
end
-- Create explosion
local function onCollision ( event )
if event.phase == "began" and bomb.y \> 375 then
bomb:play{ startFrame=3, endFrame=11, loop=1, remove=true }
audio.play( bombSound )
bomb:applyForce( 0, 0, bomb.x - 10, bomb.y )
end
end
--Add even listeners to the bomb
bomb:addEventListener( "collision", onCollision )
bomb:addEventListener("enterFrame", speedBoost)
[import]uid: 32061 topic_id: 6333 reply_id: 22359[/import]
In your hitTestObject function you still have obj1 and obj2. Don’t you need to change those to the objects you’re trying to collide?
No, he used it the right way. Don’t change the values in the function definition, change the values when you call the function. [import]uid: 12108 topic_id: 6333 reply_id: 22360[/import]
So from my code (which I posted above) how should I change it
to make it work? I tried changing the values when I call the function
but it still not working I must be doing something wrong.
Thank You for help in advance! [import]uid: 30314 topic_id: 6333 reply_id: 22368[/import]
I’m an idiot. I made the change and still no luck. Does it have anything to do with the listener?
What does “enterFrame” symbolize?
ball:addEventListener(“enterFrame”, speedBoost)
Thanks. Noob here. [import]uid: 32061 topic_id: 6333 reply_id: 22376[/import]
So did anyone get it to work? Please help if you have the solution
to this question.
Thank You, [import]uid: 30314 topic_id: 6333 reply_id: 22610[/import]
I figured it out.
local function speedBoost ( event )
if hitTestObjects(bomb, btspeed) then
bomb:applyLinearImpulse(50, 0, bomb.x, bomb.y)
end
end
bomb:addEventListener( "collision", speedBoost )
The problem for me was the event listener. Someone had said use the enterframe, but i changed it to collision.
Now that we’ve figured this out, would anyone mind helping me figure out how to lock and unlock levels of the game? I posted it to the forum. http://developer.anscamobile.com/forum/2011/02/09/lock-and-unlock-game-levels [import]uid: 32061 topic_id: 6333 reply_id: 22619[/import]
Hey 4d32petersc i`m glad you got yours to work I tried following
what you did ( change enterframe to collision ) but nothing changes.
even after the change I get same results enterframe was giving me.
You mind sharing exactly what you did and how your code looks?
I really appreciated it I been at this for days and no help 
I posted my code above so if you can tell me what I did wrong
or what I should try, that would be really great.
Thank You in advance [import]uid: 30314 topic_id: 6333 reply_id: 22623[/import]
The coding is very specific and as you know, so make sure your spaces match up exactly. Try coping and pasting the function i had and change bomb to ball. [import]uid: 32061 topic_id: 6333 reply_id: 22625[/import]
Thanks for reply/help,
I tried copy and paste and changing bomb to ball but i`m still not
having any luck
the only thing I can think of because my code
seems correct ( or so I think ) and I have tried many different changes
and none work, is if you can please post your full working code, maybe
im placing my code in a wrong order (functions, objects ect…)
Thank You very much for your help and anyone who can help me solve
my problem. [import]uid: 30314 topic_id: 6333 reply_id: 22631[/import]
Well after days and long hours of trying fix the problem
It seems it was 1 line of code I needed to add, so here it is
the solution and hopefully it helps you out!!
function hitTestObjects(obj1, obj2)
local left = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin
local right = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMin <= obj2.contentBounds.xMax
local up = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin
local down = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMin <= obj2.contentBounds.yMax
return (left or right) and (up or down)
end
local function speedBoost(event)
if hitTestObjects(obj1, obj2) then
obj1:applyLinearImpulse(1000, 0, obj1.x, obj1.y)
end
end
Runtime:addEventListener(“enterFrame”, speedBoost)
Note: my problem was I was not adding a Runtime
EventListener at the bottom of code I was putting a
obj1:addEventListener(“enterFrame”, speedBoost)
anyhow I hope this helps you and (obj1 and obj2 )
were only examples you can use anything depending
what your objects are called.
(but leave function hitTestObjects as it is do not change the
obj1 obj2)
[import]uid: 30314 topic_id: 6333 reply_id: 22822[/import]