user2343538
user2343538

Reputation: 1

Objective-C - Position Object with CGPoint from another Method

Hello i have got a question. I hope anybody could help me. I do not understand… I have a method returnPointFromArray where i have an array with values (e.g. 50.0, 100.0). Than i want to random it and then i want to use the CGPoint p in the Method drawObjectWithPoint to position my Object (object from another class) with the random value.

But the drawObjectWithPoint Method says always that the CGPoint p ist 0.0, 0,0 or he says "Use of undeclared identifier p or "instance variable hides…" .

I tried the same principle to test with int and this works.

I don´t know what i´m doing wrong.

It would be great if anybody could help me and explain what i´m doing wrong.

Thanks a lot.

.h
-(void)returnPointFromArray;
-(void)drawObjectWithPoint;


.m
-(void)returnPointFromArray
{
    NSArray *points = [];

    //Random for points

     NSUInteger *randomIndex = arc4random() % [points count];

     NSValue *val = [points objectAtIndex:randomIndex];
     CGPoint p = [val CGPointValue];
}

-(void)drawObjectWithPoint
{
    Object *myObject [[Object alloc]init];
    CGPoint pNew = p;
    myObject.position = 
    [self addChild:myObject];
}

Upvotes: 0

Views: 376

Answers (4)

SomaMan
SomaMan

Reputation: 4164

Your returnPointFromArray method returns void - just modify it -

-(CGPoint)returnPointFromArray
{
    // your code

    return p;
}

Then where you want to use p, just write

CGPoint pNew = [self returnPointFromArray];

obviously you'll have to add code which actually uses this value - your code doesn't do this at all -

myObject.position = pNew;

Upvotes: 0

Saurabh Passolia
Saurabh Passolia

Reputation: 8109

assign value directly by method call

-(void)drawObjectWithPoint
{
    Object *myObject [[Object alloc]init];
    myObject.position = [self returnPointFromArray];
    [self addChild:myObject];
}

-(CGPoint)returnPointFromArray  {
    NSArray *points = [];

    //Random for points

     NSUInteger *randomIndex = arc4random() % [points count];

     NSValue *val = [points objectAtIndex:randomIndex];
     return [val CGPointValue]; 
}

Upvotes: 1

Ahmed Z.
Ahmed Z.

Reputation: 2337

You can do this: Edited: (This is how yo declare in .h file)

in .h file

#import <UIKit/UIKit.h>

@interface MyClass : UIViewController {
    CGPoint p;
}
-(void)returnPointFromArray;
-(void)drawObjectWithPoint;
@end

in .m file

-(void)returnPointFromArray  {
    NSArray *points = [];

    //Random for points

     NSUInteger *randomIndex = arc4random() % [points count];

     NSValue *val = [points objectAtIndex:randomIndex];
     p = [val CGPointValue]; // change here
}

-(void)drawObjectWithPoint  {
    Object *myObject [[Object alloc]init];
    CGPoint pNew = p;
    myObject.position = 
    [self addChild:myObject];
}

Upvotes: 1

Girish
Girish

Reputation: 4712

Just declared CGPoint p; in .h file. You declared it as locally(returnPointFromArray function) means its scope is local for that function only. check here for reference.

Upvotes: 0

Related Questions