I am creating a basic game for test purpose in unity3D where i will counter the number of objects whenever the collision occurs on the defined object. I have created objects named birds1 upto birds8 While hitting the objects with name birds1, birds2. I am trying to increment the counter, it should increment the value of f by 1, if the hit the bird here the counter increments by random number instead of increment by 1. I have set a value that the counter should not exceed a limit of 8 so it stops at a limit of 8. Below is my code i am posting the code that i am using. Please can anyone help me with this that where is my code running wrong.
public void OnTriggerEnter2D(Collider2D target)
{
if (target.gameObject.name == "bird1" || target.gameObject.name == "bird2" || target.gameObject.name == "bird3" || target.gameObject.name == "bird4" || target.gameObject.name == "bird5" || target.gameObject.name == "bird6" || target.gameObject.name == "bird7" || target.gameObject.name == "bird8" && f < 8)
{
f++;
propPAnel8.SetActive(true);
BirdText.text = "x " + f;
if (f == 8)
{
bound8.isTrigger = true;
}
}
}
OnTriggerEnter2D will be called multiple times, especially if you have colliders with multiple contact points: That is why the increment in f it appears seemingly random, however, in this case, it is not.
You get increments as much as your contact points during the collision. What are you trying to achieve with the increment of 1? Maybe you need a boolean instead and setting isTrigger property of your Collider2D to false would help, but that depends on your game logic.
Related
Here is the code, I'm not sure why this isn't working. I've done things very simmilar before.
float limit = 1f;
float i = 0f;
if(hasResetReload == true && i < limit)
{
i += Time.deltaTime;
}
else if(i == limit)
{
hasResetReload = false;
anim.SetBool("Reload", false);
}
When you write i += Time.deltaTime;, it will assign a 7 digit long float value to i. Now, when you say i == limit, it will have to match all 7 digits on both float which is almost impossible and never become true. You have to use i >= limit. So, it will become true whenever i become greater than or equal to limit.
How do I model a boolean AND-evaluation in Promela non-atomically ?
I want to model the statement while (flag[j] == true && victim == i) where the AND-evaluation should be done non-atomically.
How is this possible?
loop:
if
:: flag[j] != true -> goto done
:: else
fi;
if
:: victim != i -> goto done
:: else
fi;
/* body of 'while' */
goto loop;
done:
/* after 'while' */
Note, although I don't have a Spin environment in front of me, what makes you think the conditional is atomic in the first place? I'd test with something like:
byte i = 0
i++ >= 0 && i-- >= 0
never { assert (1 == i) }
But, either way, the first code above shows how to make it non-atomic if need be.
I had a programmer write a Perl script for my site.
One of the functions is to update price/stock when a certain condition is met.
# update when price/stock conditions met
if ( ($force_price_updates == 1) ||
($data->{'price'} <= $product_price && $data->{'quantity'} > 0) ||
($product_quantity == 0 && $data->{'quantity'} > 0) ) {
What the above is not doing is not updating the price if the new price is higher. It updates the stock value, but if the new stock comes at a higher price, I lose out. Stock gets updated and but the price is not.
The script goes through a number of feeds and if the same product is found in any of the feeds, the script should amend price/stock change according to the rule above.
I can't find the programmer and my Perl knowledge is limited. I understand what the code is doing, but don't know what it should do if the price is higher and stock is greater than zero.
You can add the extra condition you're looking for to that statement.
The condition you're looking to match is:
$data->{'price'} > $product_price && $product_quantity > 0
So the final version would look like this:
if (($force_price_updates == 1) || ($data->{'price'} <= $product_price && $data->{'quantity'} > 0) || ($product_quantity == 0 && $data->{'quantity'} > 0) || ($data->{'price'} > $product_price && $product_quantity > 0)) {
I am trying to run a "Walk" style animation on my main game sprite. The animations work fine, and my sprite is hooked up to my joystick all fine and dandy.
However, I think where I setup the call for my walk animations are wrong. Because everytime the sprite is moving, the animation stops.
I know putting the animation in such a weak if statement is probably bad, but please tell me how I could get my sprite to animate properly while it is being moved by the joystick.
The sprite faces the right direction, so I can tell the action's first frame is being called, however, it doesn't animate until I stop touching my joystick.
Here is How I call the action:
//WALK LEFT
if (joypadCap.position.x <= 69 /*&& joypadCap.position.y < && joypadCap.position.y > >40 */ )
{
[tjSprite runAction:walkLeft];
};
//WALK RIGHT
if ( joypadCap.position.x >= 71 /* && joypadCap.position.y < 100 && >joypadCap.position.y > 40 */)
{
[tjSprite runAction:walkRight];
};
THIS: is the how the joystick controls the character:
CGPoint newLocation = ccp(tjSprite.position.x - distance/8 * cosf(touchAngle),
tjSprite.position.y - distance/8 * sinf(touchAngle));
tjSprite.position = newLocation;
Please help. Any alternative ways to call the characters walk animation would be greatly appreciated!
int current_state;
if (current_state != 1 && joypadCap.position.x <= 69)
{
current_state = 1;
[tjSprite runAction:walkLeft];
}
else if (current_state != 1 && joypadCap.position.x >= 71)
{
current_state = 1;
[tjSprite runAction:walkRight];
}
else
{
current_state = 0;
//[tjSprite stopAllActions];
};
The sprite faces the right direction,
so I can tell the action's first frame
is being called, however, it doesn't
animate until I stop touching my
joystick.
Based on the code you have supplied this actually makes sense. What your if statement is saying is anytime the joypadCap position is greater than 71 or less than 69 play an animation. This means your animations will try to play over and over again from the beginning everytime joypadCap's position falls in these ranges. I assume joypadCap is a measure of how much the joystick is being pressed?
Looks like you could use some additional state logic to determine what your character should be doing. Here is some pseudocode...
state current_state;
if (current_state != walking and joypadCap.position.x <= 69)
{
current_state = walking;
[tjSprite runAction:walkLeft];
}
else if (current_state != walking and joypadCap.position.x >= 71)
{
current_state = walking;
[tjSprite runAction:walkRight];
}
else
{
current_state = idle;
[tjSprite stopAllActions];
}
Keep in mind that is loose pseudocode. Not everything is syntactically correct but logically the idea is that you have a state variable to keep track of the characters current state which allows you so have your animations play one time only. Let me know if this helps and if you have any questions about my answer.
what is the proper way to do If statement with two variable in Xcode for Iphone
currently I have
if (minute >0) && (second == 0) {
minute = minute - 1;
second = 59;
}
The same was you would do it in any C/C++/Objective-C compiler, and most Algol derived languages, and extra set of parenthesis in order to turn to seperate boolean statements and an operator into a single compound statement:
if ((minute > 0) && (second == 0)) {
minute = minute - 1;
second = 59;
}
You'll need another set of parenthesis:
if ((minute >0) && (second == 0)) {
minute = minute - 1;
second = 59;
}
Or you could also write:
if (minute > 0 && second == 0)
Which is what you'll start doing eventually anyway, and I think (subjective) is easier to read. Operator precedence insures this works...