IF OR statement

Hi guys,

I think I’m on the verge of pulling my hair out :slight_smile:
Can anyone please tell me why this won’t work?

[code]
local function onLocalCollision( self, event )
if ( event.phase == “began”) then
if ( event.other.name ~= “item 1” or event.other.name ~= “item 2” ) then

– do stuff

end
end
end
[/code] [import]uid: 40538 topic_id: 13803 reply_id: 313803[/import]

I might be wrong but I would do something like this.
[lua]local function onLocalCollision( self, event )
if ( event.phase == “began”)and event.other.name == “item 1” then
– do stuff
elseif ( event.phase == “began”) and event.other.name == “item 2” then
– do stuff

end
end
end[/lua] [import]uid: 39088 topic_id: 13803 reply_id: 50670[/import]

[lua] local function onLocalCollision(event )
if ( event.other.name ~= “item 1” ) or (event.other.name ~= “item 2” ) then
print(“something”)

end
end[/lua]

this will work [import]uid: 16142 topic_id: 13803 reply_id: 50673[/import]

how are you registering the event ?
can you post the adEventListener line ? [import]uid: 71210 topic_id: 13803 reply_id: 50675[/import]

[code]

player.collision = onLocalCollision
player:addEventListener( “collision”, player)

[/code] [import]uid: 40538 topic_id: 13803 reply_id: 50678[/import]

ok thats it…
did you mean other object not item 1 and item 2 ?
try putting “and” in your if condition then. [import]uid: 71210 topic_id: 13803 reply_id: 50683[/import]

Yes, that’s what I meant.

Although it seems weird, putting an “and” in there seems to have fixed it.
I still cannot get the collision to only fire once though…
Cheers for your help! [import]uid: 40538 topic_id: 13803 reply_id: 50687[/import]

the correct operator shuld be “and” if you use “or” that condition will be true for all name.
if name is item1 2nd part of condition will be true and if its item2 1st part of condition will be true and in both case “or” will give true. so the condition is true for item1 and item2. giving “and” will solve the issue.

and for your next problem… if you give the above condition we can’t do anything even for the first collision.

if you can tell me what you are trying to achieve i will try to do my best to help you. :slight_smile:
[import]uid: 71210 topic_id: 13803 reply_id: 50691[/import]

Thanks for the explanation :slight_smile:
OK, what I’m trying to do is have 2 objects which the player can collect.
But I think I may have fixed it… [import]uid: 40538 topic_id: 13803 reply_id: 50725[/import]

good to know that your issue is solved… [import]uid: 71210 topic_id: 13803 reply_id: 50732[/import]