Hey,
I am new to corona (and lua).
I was wondering how I can make a invisible rectangle that that when my object (it is called ball) moved over it, it would change the scene (to level_2.lua). I have implemented physics.
So, what code do I need to create a invisible rectangle that on contact with another object (ball) will change?
His code example worked great! I am a bit of a novice to all this… so my first question… I was wondering how I can use this (code that you showed me). I want to make a invisible box:
local img2 = display.newRect(200, 100, 50, 50)
How can I make it invisible?
Now My 2nd object is a “ball” and I makes this in a different file:
local ball = require("ball")
How can I make it so that when “ball” makes contact with “img2” it changes scene (I will use director class). Would it look something like this? (I my be far off!!) :
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 ball = require("ball")
local img2 = display.newRect(200, 100, 50, 50)
local function drag(event)
if event.phase == "moved" then
ball.x = event.x
ball.y = event.y
print(hitTestObjects(ball, img2))
end
end
ball:addEventListener( "enterFrame", movement )
I think this code is just a huge mess! So how can I do this? Also, (as you can see!) I am trying to use the Accelerometer… what should I put as the event listener?
I don’t know anything about Director, but I can see in your code that you store the “ball” module in the variable “ball”, not a display object. That is, this line stores the module, not the display object:
local ball = require("ball")
You need to get the display object you created in the module.
Thanks Mr. J!
Y’know, there’s a like button on that page. [import]uid: 12108 topic_id: 7176 reply_id: 25780[/import]
Ok, sorry I am a bit of a noob to all this!
So, (tell me if I am wrong) I need to “call for” the ball (instead of the whole module)? Is that right?
How can I do this? What would my code look like to get “just” the ball?
local ballMod = require("ball")
local ballImg = ballMod.ball
but what exactly you need depends on what exactly is going on in your module. I mean, a module is just another page of code and you could have written anything in there. [import]uid: 12108 topic_id: 7176 reply_id: 26104[/import]
Hey,
This is what is currently in my file (the file that I am trying to add objective to):
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
--
-- Setup Physics
local physics = require ("physics")
physics.start()
physics.setGravity (0,0)
--
-- Load Ball
local ballMod = require("ball")
local ballImg = ballMod.ball
-- Movement Function
local function movement()
-- Get X & Y Accelerometer Input From Remote
local xAcceleration = remote.xGravity
local yAcceleration = remote.yGravity
-- Convert Input To Force
local xForce = xAcceleration \* 55
local yForce = yAcceleration \* 55
-- Apply Force To Ball
ball.applyForceToBall( xForce , -yForce )
end
Runtime:addEventListener( "enterFrame", movement )
--
local objective = display.newRect(200, 100, 50, 50)
objective:setFillColor(255, 255, 255)
objective.alpha = 0
print(hitTestObjects(ball, objective))
Is everything okay here? (meaning the only logical problem is that I am not getting the “ball” module correctly).
Thanks for your patience… sorry I am a bit of a noob!
PS thanks for the help making it invisible… it worked great! [import]uid: 15281 topic_id: 7176 reply_id: 26218[/import]
I am sorry for wasting your time.
If you fell inpatient with my level of knowledge, please do not respond to my questions.
For me (being a beginner), this sort of negativity is not helpful. If you want to answer my questions then that is great!
[lua]-- Apply Force To Ball
ball.applyForceToBall( xForce , -yForce )[/lua]
you havent defined “ball” anywhere, so that’s going to be nil and that line will fail. you did have “ball” defined before. not sure why you took it out.
and we have no idea what’s going on in your ball module, so can’t help with how to reference the functions/objects in there. you’ll need to give us a little more info
For me (being a beginner), this sort of negativity is not helpful.
You misunderstood me. I wasn’t bothered by your questions (after all, if your questions annoyed me then I just wouldn’t respond) but suggesting that if you are a newcomer then you should be posting in the Newcomers forum. I mean, in case you didn’t notice that. It was meant as a helpful hint, not as negativity.
ps Everyone starts somewhere.
That is true. Specifically, everyone is supposed to start in the Newcomers forum.
As for the code you posted, that is not inside the “ball” module. We need to see that code, the code in ball.lua
Also, what jmp said about the variable “ball” not being defined anymore. You changed the name when declaring the variable, but you didn’t also change the references to that variable.
oh incidentally
not sure why you took it out.
He took it out because he pasted what I wrote over it. Nevermind that I said “something like,” not “this exact text.” [import]uid: 12108 topic_id: 7176 reply_id: 26329[/import]
@jhocking
I am sorry if I misunderstood you. In hind sight, I fell that I did not give the necessary information (the ball module). As for the ball module, this is what is inside:
--------------------ball--------------------
-- Load Shadow
local shadow = display.newCircle( 160 , 120 , 16 )
shadow:setFillColor( 0 , 0 , 0 )
shadow.alpha = 0.2
shadow.yScale = 1
-- Load Ball
ballObject = display.newImageRect( "ball.png" , 30 , 30 )
ballObject.x = 50
ballObject.y = 430
physics.addBody( ballObject , { density = 1.388997268825 , friction = 4, bounce = 0.1 , radius = 15 } )
-- Apply Force To Ball
function applyForceToBall( xForce , yForce )
-- Apply Force To Ball
ballObject:applyForce( xForce , yForce , ballObject.x , ballObject.y )
-- Move Ball To Front
ballObject:toFront()
end
-- Follow Ball
local function followBall()
-- Move Shadow To Ball
shadow.x = ballObject.x
shadow.y = ballObject.y + 5
end
Runtime:addEventListener( "enterFrame" , followBall )
After seeing this, is this what my code for getting the ball should look like:
-- Load Ball
local ballMod = require("ball")
local myball = ballMod.ballObject
Also, this will not get my Shadow will it?
Thank you.
–
@jmp909
Also, thanks jmp909 I have changed my code to what you said (I totally forgot to change the code (yes, I just copied and pasted)) [import]uid: 15281 topic_id: 7176 reply_id: 26693[/import]
Ok, How would I make a display group?
Would this be done in the file that I am trying to use it?
Also, what is the code I need to make it? Would it be like this:
local ballMod = require("ball")
local ball = ballMod.ballObject
local shadow = ballMod.shadow
local group = display.newGroup()
group:insert( ball )
group:insert( shadow )
you could do it that way. but why wouldnt you do it in your ball module and treat it as one object? [import]uid: 6645 topic_id: 7176 reply_id: 26727[/import]