y won't this code work

Non-platform specific questions.

y won't this code work

Postby 157pl » Fri Jul 02, 2010 4:13 am

i tried this
if(direction(player.x,player.y,aim.x,aim.y)>=direction(player.x,player.y,enemy.x,enemy.y+10))
if(direction(player.x,player.y,aim.x,aim.y)<=direction(player.x,player.y,enemy.x,enemy.y-10))
DestroyActor("enemy");
this
if(direction(player.x,player.y,aim.x,aim.y)<=direction(player.x,player.y,enemy.x,enemy.y+10))
if(direction(player.x,player.y,aim.x,aim.y)>=direction(player.x,player.y,enemy.x,enemy.y-10))
DestroyActor("enemy");
this
if(direction(player.x,player.y,aim.x,aim.y)>=direction(player.x,player.y,enemy.x,enemy.y-10))
if(direction(player.x,player.y,aim.x,aim.y)<=direction(player.x,player.y,enemy.x,enemy.y+10))
DestroyActor("enemy");
and this
if(direction(player.x,player.y,aim.x,aim.y)<=direction(player.x,player.y,enemy.x,enemy.y-10))
if(direction(player.x,player.y,aim.x,.aim.y)>=direction(player.x,player.y,enemy.x,enemy.y+10))
DestroyActor("enemy");
none of them work :cry:
i am trying to kill the enemy when he gets shot by player but i don't want to draw the bullet
User avatar
157pl
 
Posts: 109
Joined: Thu May 13, 2010 10:49 pm
Location: AZ
Score: 3 Give a positive score

Re: y won't this code work

Postby Bee-Ant » Fri Jul 02, 2010 6:23 am

157pl wrote:i am trying to kill the enemy when he gets shot by player but i don't want to draw the bullet

Hmmm...in this case, each game type should has different method. On platform, you need the y matching and the shot range. On fps you need the x and y matching. etc...
So, before I give my suggestion, may I see your game screenshot?
I need to know how's your player and his enemy...
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: y won't this code work

Postby Fuzzy » Tue Jul 06, 2010 3:31 pm

Looks overly complicated. This usually sets off alarm bells in my mind, as that is usually a great place for bugs to hide. In this case it seems true.

Code: Select all
if(direction(player.x,player.y,aim.x,aim.y)>=direction(player.x,player.y,enemy.x,enemy.y+10))
if(direction(player.x,player.y,aim.x,aim.y)<=direction(player.x,player.y,enemy.x,enemy.y-10))
DestroyActor("enemy");


We will ignore all the others for now, but i will give you a pointer in reducing your code when we solve this much.

First, we have to simplify to see whats going on. We substitute some simple numbers to clean up the example. We will pretend to solve direction for instance. it doesnt really matter what we put in there. Since the actor variables cannot change between the ifs, we must make them the same. Here is my reduced example.

Code: Select all
if(5>=6)
if(5<=6)
DestroyActor("enemy");


Now we want to reduce it again to true/false terms as that is what if does. Since 5 is never greater or equal to 6, its false. and since without changing the variables - you cant between the ifs() - 5<=6 is true.

Code: Select all
if(false)
if(true)
DestroyActor("enemy");


if its false, and its true, DestroyActor("enemy")

See the problem? this is exactly like just saying DestroyActor("enemy"). You might as well leave the ifs out, cause its always going to destroy. I bet the enemy actor never appears, right?

Which isnt what you want. In fact, the way you have written it causes the first if() to always be ignored for the second one.

If you go back and flip the 5s and 6s, you will find that the actor still always gets destroyed for the reason listed above plus one more even more important reason. You are using if() incorrectly.

if(5>=6) {
DestroyActor("enemy");
}

You need those curly brackets because they attach the action - DestroyActor() - to the if(). Without them the action always happens... it is not dependent on the result of the if() statement.

It is like a paragraph when you are writing a story. Like html markup codes around a section of text. If() needs them to understand what belongs to it.

If you rewrite this code correctly you will still see a problem.

if(5>=6) {
DestroyActor("enemy");
}
if(5>=6) {
DestroyActor("enemy");
}

Your actor still always dies. Why? because the first term in the if is always greater, lesser or equal to the second term. You have covered all the bases, and it doesnt really matter what numbers you use, "enemy" actor is always going to be destroyed. This is a design bug and GE and other programming languages will not complain about them, but you wont get the effect you want.

I hope this clears some things up for you, and if you have any questions about what I have said, ask.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest