0

こんにちは、質問があります。誰かが私を助けてくれることを願っています。わかりません… returnPointFromArray メソッドがあり、値を持つ配列があります (例: 50.0、100.0)。それをランダムにしたいので、メソッド drawObjectWithPoint で CGPoint p を使用して、オブジェクト (別のクラスのオブジェクト) をランダムな値で配置します。

しかし、drawObjectWithPoint メソッドは常に CGPoint p ist 0.0, 0,0 と言うか、「宣言されていない識別子の使用pまたは "インスタンス変数が非表示になる…" と言っています。

int でテストするために同じ原則を試しましたが、これは機能します。

何が間違っているのかわかりません。

誰かが私を助けて、私が間違っていることを説明してくれれば、それは素晴らしいことです.

どうもありがとう。

.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];
}
4

4 に答える 4

1

これを行うことができます: 編集済み:(これは.hファイルで宣言する方法です)

.h ファイル内

#import <UIKit/UIKit.h>

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

.m ファイルで

-(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];
}
于 2013-05-15T07:06:19.333 に答える
1

メソッド呼び出しで値を直接代入する

-(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]; 
}
于 2013-05-15T07:16:43.903 に答える
0

CGPoint p;.h ファイルで宣言するだけです。local ( function) として宣言したのreturnPointFromArrayは、そのスコープがその関数に対してのみローカルであることを意味します。こちら を参考にチェック。

于 2013-05-15T07:07:19.600 に答える