floatValue for CGFloat - cocoa

I have a NSSlider that is attempting to convert a regular float into a CGFloat. The code that I am using:
CGFloat testing = [mainSlider floatValue];
However this simply makes the testing variable "0.00" when I attempt to log it.
NSLog(#"%f", testing);
Does anyone have any ideas as to why this would happen? Thanks for any help.

check that mainSlider isn't nil
try this just in case (can't remember if CGFloat is a double or not):
NSLog(#"%f", (double)testing);
Check that the variable isn't being shadowed like this:
CGFloat testing = 0.0;
if(YES){
CGFloat testing = [mainSlider floatValue];
//should be: testing = [mainSlider floatValue];
}
NSLog(#"testing = %f", testing); //this will print "testing = 0.000000"

Related

SpriteKit Animation Physics

is there a way to apply skphysics to a animated character? Okay I have my player play an animation when I move it around the screen and below is it's animation code but for some reason when I want to apply physics to my animation, I get an error saying wrong data type.
Sending 'SKSpriteNode *_strong' to parameter of incompatible type 'CGRect' (aka 'struct CGRect')
NO Reputation and I can't answer my own question so I posted here.
actually never mind I had to add this _Player.frame and I am hoping this is right and is putting physics on my character. However, please correct me if I am wrong. thanks.
- (void) Player {
_Player = [SKSpriteNode spriteNodeWithImageNamed:#"bird"];
_Player.xScale = 0.88;
_Player.yScale = 0.88;
_Player.position = CGPointMake(100, 100);
[self addChild:_Player];
// 1
NSMutableArray *PlayerCh = [NSMutableArray arrayWithCapacity:2];
// 2
for (int i = 1; i < 2; i++) {
NSString *mytextureName = [NSString stringWithFormat:#"bird%d", i];
SKTexture *ItsTexture = [SKTexture textureWithImageNamed:mytextureName];
[PlayerCh addObject:ItsTexture];
}
// 3
for (int i = 2; i > 1; i--) {
NSString *mytextureName = [NSString stringWithFormat:#"bird%d", i];
SKTexture *ItsTexture = [SKTexture textureWithImageNamed:mytextureName];
[PlayerCh addObject:ItsTexture];
}
// 4
_PlayerAnimation = [SKAction animateWithTextures:PlayerCh timePerFrame:0.1];
// SKPHYSICS FOR PLAYER
SKPhysicsBody *MYPLAYER = [SKPhysicsBody bodyWithCircleOfRadius:_Player]; // I get a error here.
}
Of course this program has to crash when it runs
[SKPhysicsBody bodyWithCircleOfRadius:_Player] expects a float not an SKPriteNode, what you probably want to do is change this line of code to the following:
SKPhysicsBody *MYPLAYER = [SKPhysicsBody bodyWithCircleOfRadius:_Player.size.width/2];

Multiplying double variable by double in Xcode

I'm making in app that includes a user entering the price of an object, and the app outputs the price including tax. However, every time it outputs 0, and I get a run-time error involving the variable, myDouble. I'm trying to take the value put in a text field and multiplying it by 1.06 (I'm starting out with a set tax rate), and then setting a label to the new value. Here is my code:
-(IBAction)textFieldReturn:(id)sender
{
[sender resignFirstResponder];
NSString *myString = inputtext2.text;
double myDouble = [myString doubleValue];
myDouble = myDouble*1.06;
NSLog(#"myDouble: %lf", myDouble);
price.text = [NSString stringWithFormat:#"%g", myDouble];
}
Most likely your inputtext2 variable is nil, so it is returning nothing. If this is an outlet, check the connections in interface builder. If the outlet is not connected, the variable will be nil, so the string value will be nil, so the double value will be 0.
I think you are going to want to use a NSNumberFormatter and check for valid entry.
pseudo code, uncompiled:
-(IBAction)textFieldReturn:(id)sender
{
[sender resignFirstResponder];
NSString *myString = inputtext2.text;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle: NSNumberFormatterDecimalStyle];
NSNumber *myNumber = [formatter numberFromString: myString];
double myDouble = 0;
if (myNumber != nil) {
myDouble = [myNumber doubleValue];
}
NSString *outputString = [formatter stringFromNumber:myDouble];
price.text = outputString;
}
This should default your number to 0 if the text entered is 0 or not a valid number.

IBAction float, have a equal different numbers depending on input

New to objective-c and I just can't find the answer to this question anywhere. I'm running a IBAction for a calculation but I need the input from a label to actually equal something else in the equation.
For example:
-(IBAction)calculate; {
float a = 1.05 if ([label1.text] == 1in);
a = 2.07 if ([label1.text] == 2in);
a = 3.07 if ([label1.text] == 3in);
float b = a*([textField1.text floatValue]);
label2.text = [[NSString alloc] initWithFormat:#"%2.f", b];
}
I know I'm not even close to getting it right but I hope you get the idea as to what I'm looking for.
Thank you!
- (void)calculate {
float a;
if ([self.inputField.text isEqualToString:#"1in"]) a = 1.05f;
else if ([self.inputField.text isEqualToString:#"2in"]) a = 2.07f;
else if ([self.inputField.text isEqualToString:#"3in"]) a = 3.07f;
else a = 0.0f;
if (a) {
float b = a * ([self.inputField.text floatValue]);
self.inputField.text = [[NSString alloc] initWithFormat:#"%.2f", b];
}
else self.inputField.text = #"Invalid";
}
- (void)viewDidLoad
{
//...
self.inputField = [[UITextField alloc] initWithFrame:CGRectMake(10.0f, 20.0f, 300.0f, 30.0f)];
[self.inputField setBackgroundColor:[UIColor grayColor]];
[self.view addSubview:self.inputField];
UIButton * doneButton = [[UIButton alloc] initWithFrame:CGRectMake(10.0f, 55.0f, 300.0f, 30.0f)];
[doneButton setBackgroundColor:[UIColor blackColor]];
[doneButton setTitle:#"Done" forState:UIControlStateNormal];
[doneButton addTarget:self action:#selector(calculate) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:doneButton];
[doneButton release];
}
Note: I replaced the output format %2.f to %.2f, cause I guess you may need this format. E.g:
Input > 1in
Output< 1.05
Input > 2in
Output< 4.14
Input > 3in
Output< 9.21

Cocos2D: Passing a CGPoint through CCCallFuncND

What is the appropriate way to call a method with an action, and what should the method itself look like for passing a CGPoint parameter? I've tried to look up examples online without much luck, so I've been pretty much guessing.
What I have tried is this for calling it:
CGPoint spriteCoord = saveStation.sprite.position;
id a1=[CCMoveTo actionWithDuration:.4 position:ccp(saveStation.sprite.position.x,saveStation.sprite.position.y)];
id actionSaveStationReaction = [CCCallFuncND actionWithTarget:self selector:#selector(saveStationReaction : data:) data:&spriteCoord];
[hero.heroSprite runAction:[CCSequence actions:a1, actionSaveStationReaction, nil]];
And the method itself:
-(void) saveStationReaction:(id)sender data:(void *)data {
CGPoint spriteCoord = (void *)data; //error: Invalid initializer
NSLog(#"spriteCoord x = %f", spriteCoord.x);
NSLog(#"spriteCoord y = %f", spriteCoord.y);
}
The proper way to send a CGPoint (or any non-id type like C structs) to a method that takes an id as parameter (any method that uses performSelector) is by wrapping it in an NSValue object:
NSValue* value = [NSValue valueWithBytes:&spriteCoord objCType:#encode(CGPoint)];
In the method that is being called you can retrieve the point from the NSValue object by casting the data pointer to NSValue* and calling getValue:
-(void) saveStationReaction:(id)sender data:(void *)data {
CGPoint spriteCoord;
[((NSValue*)data) getValue:&spriteCoord];
NSLog(#"spriteCoord x = %f", spriteCoord.x);
NSLog(#"spriteCoord y = %f", spriteCoord.y);
}
GamingHorror's suggestion on wrapping the CGPoint in an NSValue is spot-on.
But there's a simpler way than using valueWithByte:objCType: method: valueWithCGPoint:, assuming you are coding for iOS and not MacOS.
NSValue *spriteCoordValue = [NSValue valueWithCGPoint:spriteCoord];
[CCCallFuncND actionWithTarget:self selector:#selector(saveStationReaction:data:) data:spriteCoordValue];
// and ..
-(void) saveStationReaction:(id)sender data:(void *)data {
CGPoint spriteCoord = [(NSValue *)data CGPointValue];
NSLog(#"spriteCoord x = %f", spriteCoord.x);
NSLog(#"spriteCoord y = %f", spriteCoord.y);
}
NSValue can also deal with CGSize and CGRect using similar way.
You can't typecast to a CGPoint with that syntax. Try....
CGPoint *spriteCoord = data;
CGFloat ptX = spriteCoord->x;
CGFloat ptY = spriteCoord->y;
I tried...
CGPoint* spriteCoord = (CGPoint)data;
which didn't work and I guess expectedly so. Try my first suggestion and see if that works for you. It did compile for me but I'm not sure how it will execute and that may depend on your particular situation.
CGPoint is a struct, not an object, so you can't pass it directly to any of the CCCallFunc's. There are several ways of dealing with this, but the quickest converts the CGPoint to NSString using NSStringFromCGPoint, passing the string, then converting it back to a CGPoint using CGPointFromString.

EXC_BAD_ACCESS error in Xcode

I really need you guy HELP , I run my program in Xcode and its successful but later,
Its show me this error: **Thread 1: Program received signal :"EXC_BAD_ACCESS" on my program line that I have **bold below :
- (NSString *) ocrImage: (UIImage *) uiImage
{
CGSize imageSize = [uiImage size];
double bytes_per_line = CGImageGetBytesPerRow([uiImage CGImage]);
double bytes_per_pixel = CGImageGetBitsPerPixel([uiImage CGImage]) / 8.0;
CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider([uiImage CGImage]));
const UInt8 *imageData = CFDataGetBytePtr(data);
// this could take a while. maybe needs to happen asynchronously.
**char* text = tess->TesseractRect(imageData,(int)bytes_per_pixel,(int)bytes_per_line, 0, 0,(int) imageSize.height,(int) imageSize.width);**
// Do something useful with the text!
NSLog(#"Converted text: %#",[NSString stringWithCString:text encoding:NSUTF8StringEncoding]);
return [NSString stringWithCString:text encoding:NSUTF8StringEncoding];
}
Thank you guy .
make sure that imageData is not NULL here. That's the most common cause of what you're seeing. You should reconsider your title to something more related to your problem, and focus on the stacktrace and all the variables you are passing to TesseractRect().
The other major likelihood is that tess (whatever that is) is a bad pointer, or that is not part of the correct C++ class (I assume this is Objective-C++; you're not clear on any of that).
- (NSString *)readAndProcessImage:(UIImage *)uiImage
{
CGSize imageSize = [uiImage size];
int bytes_per_line = (int)CGImageGetBytesPerRow([uiImage CGImage]);
int bytes_per_pixel = (int)CGImageGetBitsPerPixel([uiImage CGImage]) / 8.0;
CFDataRef data =
CGDataProviderCopyData(CGImageGetDataProvider([uiImage CGImage]));
const UInt8 *imageData = CFDataGetBytePtr(data);
// this could take a while. maybe needs to happen asynchronously?
char *text = tess.TesseractRect(imageData, bytes_per_pixel, bytes_per_line, 0,
0, imageSize.width, imageSize.height);
NSString *textStr = [NSString stringWithUTF8String:text];
delete[] text;
CFRelease(data);
return textStr;
}

Resources