このトピックはここに 100 の投稿でカバーされていることは知っていますが、この特定のインスタンスで多くの問題が発生しており、理解できません。
基本的に、Spritebuilder を使用してスプライト/ノードをゲームにインポートしています。クラスの本体に特定のクラスのスプライトをインポートしGameScene
ますが、スプライト クラス内で変数を定義し、クラスから編集できるようにしたいと考えていますGameScene
。たとえば、スプライトが GameScene 内でコインを収集する場合、update
スプライト クラスのメソッド内でスプライトの速度を変更したいと考えています。
以下は私のコードですが、残念ながら機能していません。変数increaseY
とは、私のクラスincreaseX
では利用できないようです。GameScene
これは、クラスを適切にインスタンス化していないためであることはわかっていPenguin
ますが、.ccbi ファイルを同時にインポートしながら、このクラスのインスタンスを適切に作成する方法がわかりません。問題の行にはコメントが付けられており、簡単に見つけられるようにその横にたくさんの**が付いています。ですGameScene.m
。私は助けに本当に感謝しています.これに数時間立ち往生しています.
ペンギン.h
#import "CCSprite.h"
@interface Penguin : CCSprite
{
float xPosition;
float yPosition;
}
@property (nonatomic,assign) float increaseY;
@property (nonatomic,assign) float increaseX;
@end
ペンギン.m
#import "Penguin.h"
@implementation Penguin
@synthesize increaseX;
@synthesize increaseY;
- (id)init {
self = [super init];
if (self) {
CCLOG(@"Penguin created");
}
return self;
}
-(void) update:(CCTime)delta
{
self.position = ccp(self.position.x + increaseX,self.position.y + increaseY);
}
@end
GameScene.h
#import "CCNode.h"
@interface GameScene : CCNode
@end
GameScene.m
#import "GameScene.h"
#import "Penguin.h"
@implementation GameScene
{
CCPhysicsNode *_physicsNode;
CCNode *_catapultArm;
CCNode *_levelNode;
CCNode *_contentNode;
}
// is called when CCB file has completed loading
- (void)didLoadFromCCB {
self.userInteractionEnabled = TRUE;
CCScene *level = [CCBReader loadAsScene:@"Levels/Level1"];
[_levelNode addChild:level];
}
// called on every touch in this scene
- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
[self launchPenguin];
}
- (void)launchPenguin {
// loads the Penguin.ccb we have set up in Spritebuilder
CCNode* penguin = [CCBReader load:@"Penguin"];
penguin.position = ccpAdd(_catapultArm.position, ccp(16, 50));
[_physicsNode addChild:penguin];
//THE FOLLOWING LINE DOES NOT WORK********************************
penguin.increaseY = 1;
// Gives Error------Property "increaseX" not found on object of type "CCNode *"
self.position = ccp(0, 0);
CCActionFollow *follow = [CCActionFollow actionWithTarget:penguin worldBoundary:self.boundingBox];
[_contentNode runAction:follow];
}