I am writing a thread that should be stopped if a button is pressed.
This means that for every action that I do, I also want to check if the stop flag is set so I can stop properly.
This is a piece of the code which should make it clearer:
while([self checkForDrop] != 0 && STOP == FALSE){
sleep(0.4);
} if (STOP == TRUE){
return 99;
}
ret = [self doSomething]
if(ret == 0){
[self updateStatus]
} else {
printf("%d", ret) // Print error
}
ret = [self doSomethingElse]
if(ret == 0){
[self updateStatus]
} else {
printf("%d", ret) // Print error
}
... and so on ...
Every time I do something, I want to also check if a condition is met (STOP == TRUE), if it is, the program should return 99 and stop.
I could write an if statement for every line, but that would make the code unreadable and ugly. I would guess that I am not the only one that stumbled upon this, so there should be a better way.
Related
This is what I try to accomplish:
Two sprite nodes in the scene, and self is an edge loop.
If nodeA touches nobeB, and stop. >> Win
If nodeA touches self. >> Lose
If nodeA touches nobeB but didn't stop and touches self. >> Lose
Therefore I need something that works like this:
typedef NS_OPTIONS(aero, SpriteNodeCategory)
{
SpriteNodeCategoryA = 1 << 0,
SpriteNodeCategoryB = 1 << 1,
};
-(void)didBeginContact:(SKPhysicsContact *)contact
{
aero collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
if (collision == (SpriteNodeCategoryA|SpriteNodeCategoryB)) {
//something here I don't know...
}
if (collision == (SpriteNodeCategoryA|SpriteNodeCategoryB)) {
NSLog(#"FAIL");
}
}
You have another method delegate (SKPhysicsContactDelegate) which do the job you asking about:
- (void)didEndContact:(SKPhysicsContact *)contact
Okay so this is going to be a little difficult.
First of, you can use "didSimulatePhysics" to get the update of your simulation
-(void)didSimulatePhysics
{
if (_yourNode.physicsBody.angularVelocity == 0 && newGame) {
if (_yourNode.userData[#"winningCondition"]) {
[self win];
};
}
}
this does is that it updates and see if you have a winning condition - which is gotten from collision (think: reverse engineering)
P.S. _yourNode.physicsBody.angularVelocity == 0 meaning your node is completely still
-(void)didBeginContact:(SKPhysicsContact *)contact
{
uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
if (collision == (CNPhysicsCategoryNodeA|CNPhysicsCategoryNodeB)) {
_yourNode.userData = [#{#"winningCondition":#(YES)} mutableCopy];
}
if (collision == (CNPhysicsCategoryNodeA|CNPhysicsCategoryEdge)) {
[self lose];
}
}
-(void)didEndContact:(SKPhysicsContact *)contact
{
uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
if (collision == (CNPhysicsCategoryNodeA|CNPhysicsCategoryNodeB)) {
_yourNode.userData = [#{#"winningCondition":#(NO)} mutableCopy];
}
}
So basically when A touches B, it gives you a winning condition through adding a "userData" to your node.
The rest is pretty straight forward. I think you get it.
I'm going through some tutorial for editing table cells whose data is stored in a server. Everything is working fine - I can edit a table cell and click on the "save" button, but if I go back to the table overview it's not updated.
I've got 3 table fields:
titleField
authorField
atextField
I don't know if the problem comes from this piece of code, but I suppose so. The tutorial example has just 2 fields, and I need 3 fields, but I don't know how to implement this piece of code for 3 textfields:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
if (textField == titleField) {
[authorField becomeFirstResponder];
}
if (titleField == authorField) {
[self save];
}
return YES;
}
I already tried if (titleField == authorField == atextField), but the error message says: Comparison between pointer and integer ('int' and 'UITextField'). I also tried if (titleField == authorField && titleField == atextField && authorField == atextField){and I don't get an error, but it doesn't change the fact that the data doesn't update the changes.
How should the code above look like?
Those IF's dont have sense, you cannot do this:
if (titleField == authorField == atextField)
Because you are comparing the result of the first == with the textfield, hence the Comparison between pointer and integer error.
In the 2nd one,
if (titleField == authorField && titleField == atextField && authorField == atextField)
This is never going to get called, because the titleField cannot be 3 things at the same time.
My first thought would be doing something like this:
if (textField == titleField) {
[authorField becomeFirstResponder];
}
else if ((textField == authorField){
[atextField becomeFisrtResponder];
else if (titleField == atextField) {
[self save];
}
I think this is what you want to do.
i create a counting timer(on a label) and a variable that contain the label integer value(named count). also i create a function that check if my number is 7, divided by 7 or contain 7. when i try to pass my count value to the check function my app is stack. i try for a long time to find why the stack overflow is occur but i didn't success. how can i solve it?
here is my code:
-(IBAction)start:(id)sender
{
timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:#selector(updateTimer:) userInfo:nil repeats:YES];
MainInt = 0;
numbersTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(countup) userInfo:nil repeats:YES];
}
-(void)countup
{
MainInt += 1;
numbersLabel.text = [NSString stringWithFormat:#"%i", MainInt];
count = numbersLabel.text.intValue;
NSLog(#"num: %d", count);
if ([self checknum:(count) == false]) {
NSLog(#"BOOM");
}
}
-(BOOL)checknum:(int)number
{
while (number<10)
{
if(number ==7)
{
NSLog(#"boom, i=%d", number);
return true;
}
}
while (number>=10 && number<1000)
{
if(number % 7 == 0)
{
NSLog(#"boom i=%d", number);
return true;
}
if([self revese:(number)])
{
NSLog(#"boom reverse num = %d", number);
return true;
}
}
return false;
}
-(BOOL) revese:(int)number
{
if(number < 10 && number != 7)
return false;
if(((number % 10) == 7) || ((number / 10) == 7))
return true;
else {
[self revese:(number / 10)];
}
return false;
}
thanks!
The statement
if ([self checknum:(count) == false]) {
NSLog(#"BOOM");
}
is nonsense. You are effective asking
BOOL isLessThanOne = (count < 1);
if ([self checksum:isLessThanOne]) {
NSLog(#"BOOM");
}
change this for
if ([self checksum:count] == NO) {
NSLog(#"BOOM");
}
There are lots of issues but the first ones are these:
while (number<10)
//
while (number>=10 && number<1000)
You want an if/else type conditional statement here. The way you have it now, since you never adjust the value of number, you will get caught in an infinite loop if the strict conditions you are testing later in the code are not met. Something more like:
if(number<10){
// do some tests
} else if (number<1000){
// do some other tests
}
There are other issues but those are a start.
It is very hard to tell as I cannot really see what you are doing, but if you have stack overflow, it is very likely that your problem is the function revese which is recursive. I would bet that you have a certain value being passed to revese that is causing it to be called over and over again based on some of the unusual logic you are using in your conditional statements. You should really step through this function carefully with your debugger to identify why this is happening.
I´m making an iphone app in objective-c with cocos2d, in the code below I try to detect a collision and then run an animation. (The box1 is moved by touch)
When the "[self getChildByTag:d]" and "box1" collide AND overlap I get the "JUMP NOW!" displayed but I don't get the jump itself, but when the box1 is moved away from the "[self getChildByTag:d]" the jump occurs.
I understand that this probably has to do with the fact that the action is called many times, but please explain to me exactly what happens and please help me with a solution!
- (void)update:(ccTime)dt {
for (int d = lowestAvailableTag; d <= highestAvailableTag; d++) {
if ([self getChildByTag:d].position.y < (box1.position.y+45)&&
[self getChildByTag:d].position.x > (box1.position.x-45) &&
[self getChildByTag:d].position.x < (box1.position.x+45) ) {
NSLog(#"JUMP NOW!");
if ([self getChildByTag:d].position.x < 150) {
[[self getChildByTag:d] runAction:
[CCJumpTo actionWithDuration:1.5
position:ccp(240, 140) height:110 jumps:1]];
}
}
}
}
//albar
You can add some BOOL flag to detect if your jump occured. Smth like:
- (void) update:(ccTime)dt
{
if( jumpOccured == false )
{
BOOL needToJump = // your jump condition
if( needToJump == true )
{
// your jump code
jumpOccured = true;
}
}
}
by the way, if you have many possible collisions, you can use box2d to detect them
I hope this question is some what self explanatory.
This works, returns YES and NO: note the NSLog()'s
- (BOOL)dateTestCourse:(NSDictionary *)listing {
BOOL result = ([self exammpleTest] == 0) ? YES : NO;
if (result) {
NSLog(#"Passes Test");
return YES;
}
NSLog(#"Failed Test");
return NO;
}
But below always return YES? Only difference is no NSLog();
- (BOOL)dateTestCourse:(NSDictionary *)listing {
BOOL result = ([self exammpleTest] == 0) ? YES : NO;
if (result) {
// NSLog(#"Passes Test");
return YES;
}
// NSLog(#"Failed Test");
return NO;
}
Is this something to do with C? I have no idea? I might expect it always to return NO (if I shouldn't be breaking in the conditional), but surely that would return YES.
I know I should be returning result in the above examples, but I'm curious to know why.
These two blocks of code should be running the same. Are you perhaps doing a find/replace all on NSLog? That could be causing issues elsewhere, say with the exammpleTest(sic) method.
Also, the ternary operator on the second line is redundant, consider reducing that line to:
BOOL result = ([self exammpleTest] == 0);