Re: Camera Shake?
Posted: Sun Nov 07, 2010 1:07 am
So did that fix the issue you where having below (actually above this post)?Ryne wrote:EDIT: I just assigned the proper variables to box1 and box2, That was a stupid question
So did that fix the issue you where having below (actually above this post)?Ryne wrote:EDIT: I just assigned the proper variables to box1 and box2, That was a stupid question
That's a reply to my post with the 16 boolean statements.@Meeloq on Twitter wrote:@zac352 lol. I usually avoid helping people with boolean operators for exactly that reason.
At the risk of sounding like a total noob. The various posts have me confused. I think I'm too stupid to know how to use booleans. This is my code:Robin wrote:So did that fix the issue you where having below (actually above this post)?Ryne wrote:EDIT: I just assigned the proper variables to box1 and box2, That was a stupid question
Code: Select all
-- player variables
player = {
hp = 100,
status = "ALIVE",
x = 100,
y = 100,
w = 32,
h = 32,
}
-- enemy variables
enemy = {
hp = 100,
status = "ALIVE",
x = 150,
y = 150,
w = 32,
h = 32,
}
box1x = player.x
box1y = player.y
box1w = player.w
box1h = player.h
box2x = enemy.x
box2y = enemy.y
box2w = enemy.w
box2h = enemy.h
Code: Select all
function CheckCollision(box1x, box1y, box1w, box1h, box2x, box2y, box2w, box2h)
if box1x > box2x + box2w - 1 or -- Is box1 on the right side of box2?
box1y > box2y + box2h - 1 or -- Is box1 under box2?
box2x > box1x + box1w - 1 or -- Is box2 on the right side of box1?
box2y > box1y + box1h - 1 -- Is b2 under b1?
then
return false -- No collision. Yay!
else
return true -- Yes collision. Ouch!
end
end
Code: Select all
if (these 2 boxes collide) then
-- DO THIS
end
Code: Select all
if SOMETHING = true then
-- do this --
end
Code: Select all
function CheckCollision(box1x, box1y, box1w, box1h, box2x, box2y, box2w, box2h)
if
box2x<box1x+box1w and box2x>box1x and --top left
box2y<box1y+box1h and box2y>box1y or
box2x+box2w<box1x+box1w and box2x+box2w>box1x and --bottom right
box2y+box2h<box1y+box1h and box2y+box2h>box1y or
box2x<box1x+box1w and box2x>box1x and --bottom left
box2y+box2h<box1y+box1h and box2y+box2h>box1y or
box2x<box1x+box1w and box2x>box1x and --top right
box2y+box2h<box1y+box1h and box2y+box2h>box1y
then
return true --The two boxes collide
else
return false --The two boxes do not collide
end
end
Thats not the problem I'm having. There doesnt seem to be an issue with the code I'm using I just dont know how to use it, read my previous post. I want to know how to program "if" statements or other variables using the boxes likezac352 wrote:*adds comments, reposts* I'm not sure if this is helping at all.Code: Select all
function CheckCollision(box1x, box1y, box1w, box1h, box2x, box2y, box2w, box2h) if box2x<box1x+box1w and box2x>box1x and --top left box2y<box1y+box1h and box2y>box1y or box2x+box2w<box1x+box1w and box2x+box2w>box1x and --bottom right box2y+box2h<box1y+box1h and box2y+box2h>box1y or box2x<box1x+box1w and box2x>box1x and --bottom left box2y+box2h<box1y+box1h and box2y+box2h>box1y or box2x<box1x+box1w and box2x>box1x and --top right box2y+box2h<box1y+box1h and box2y+box2h>box1y then return true --The two boxes collide else return false --The two boxes do not collide end end
Code: Select all
if COLLIDE = TRUE then
-- DO THIS --
end
Code: Select all
function compare(a,b)
if a == b then
return true
end
return false
end
Code: Select all
if (condition) then
end
Code: Select all
if (condition) then
{some code}
else
{some other code}
Code: Select all
if (condition) then
{some code}
elseif (some other condition) then
{some other code}
else
{again some other code}
end
Code: Select all
1== 1
Code: Select all
1 ~= 1
Code: Select all
if myFunction(...) then
{some code}
end
Code: Select all
if collisionDetection(...) then
{do this if objects are colliding}
else
{do this if they're not, or just use end instead}
end
Code: Select all
box1x = player.x
box1y = player.y
box1w = player.w
box1h = player.h
box2x = enemy.x
box2y = enemy.y
box2w = enemy.w
box2h = enemy.h
Code: Select all
function CheckCollision(box1x, box1y, box1w, box1h, box2x, box2y, box2w, box2h)
if box1x > box2x + box2w - 1 or -- Is box1 on the right side of box2?
box1y > box2y + box2h - 1 or -- Is box1 under box2?
box2x > box1x + box1w - 1 or -- Is box2 on the right side of box1?
box2y > box1y + box1h - 1 -- Is b2 under b1?
then
return false -- No collision. Yay!
else
return true -- Yes collision. Ouch!
end
end
Code: Select all
if MyFunction = true then
-- do this --
end
Code: Select all
if myFunction() == true then -- myFunction() returned true
-- do stuff
else -- myFunction() returned false
-- do other stuff
end
Code: Select all
function CheckCollision(obj1, obj2)
if obj1.x > obj2.x + obj2.w - 1 or -- Is box1 on the right side of box2?
obj1.y > obj2.y + obj2.h - 1 or -- Is box1 under box2?
obj2.x > obj1.x + obj1.w - 1 or -- Is box2 on the right side of box1?
obj2.y > obj1.y + obj1.h - 1 -- Is b2 under b1?
then
return false -- No collision. Yay!
else
return true -- Yes collision. Ouch!
end
end
Code: Select all
if CheckCollision(player, enemy) then
-- oh, noes
end
Code: Select all
avar
math.random()
3 + 6
foo:bar(3 + 6)
unpack({1,2,3})
"Hello"
4 < x
a == b --note: a = b is NOT an expression, that is a statement.
foo and bar and x or z
There, there.Ryne wrote:
How exactly do I use this in an if statement. Like If I wanted to do something specific if the function returns true? likeThis is frustrating, and probably easier than I'm make it out to be.Code: Select all
if MyFunction = true then -- do this -- end
Code: Select all
-- declaring the function
function CheckCollision(box1x, box1y, box1w, box1h, box2x, box2y, box2w, box2h)
-- body of the function --
end
...
-- invoking the function
CheckCollision(player.x, player.y, player.w, player.h, enemy.x, enemy.y, enemy.w, enemy.h)
Code: Select all
x = 10 + 1 -- x is now 11, because 10 + 1 return 11
Code: Select all
condition1 = 3 == 4 -- condition1 is false
condition2 = 1 == 1 -- condition2 is true
Code: Select all
You should not think about it with this in mind:
if something == true then ... .
Think of it in terms of
if expression-that-returns-true then...
instead.
Code: Select all
if true then
-- this is always executed --
end
if false then
-- this is never executed--
end
Code: Select all
<, >, <=, >=, ==, ~=, and, or
Code: Select all
Also, you can make functions return true or false.
Code: Select all
if CheckCollision(player.x, player.y, player.w, player.h, enemy.x, enemy.y, enemy.w, enemy.h) then
-- do this --
end