ページ 1 / 1
Delegateの記述の仕方が良く分かりません
Posted: 2013年1月19日(土) 23:05
by kira
いつもお世話になります
現在
画面をタップ(MainFieldクラス) → Delegate → UITextView(Uilayerクラス)にテキストを表示
という処理で詰まっています
MainField.h
コード:
#import "cocos2d.h"
#import "Uilayer.h"
@protocol MainFieldDelegate;
@interface MainField : CCLayer {
id <MainFieldDelegate> delegate;
}
-(id)init;
@property (nonatomic, assign) id <MainFieldDelegate> delegate;
@end
@protocol MainFieldDelegate <NSObject>
@optional
-(void)move_text;
@end
MainField.m
コード:
#import "MainField.h"
#import "Uilayer.h"
@implementation MainField
@synthesize delegate;
-(id) init {
if ((self=[super init])) {
}
return self;
}
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
return YES;
}
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event { //画面タップ終了後Delegateに通知
if ([delegate respondsToSelector:@selector(move_text:)]) {
[delegate performSelector:@selector(move_text:)];
}
CGPoint touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
CGPoint playerPos = _player.position;
CGPoint diff = ccpSub(touchLocation, playerPos);
if (abs(diff.x) > abs(diff.y)) {
if (diff.x > 0) {
playerPos.x += _tileMap.tileSize.width;
} else {
playerPos.x -= _tileMap.tileSize.width;
}
} else {
if (diff.y > 0) {
playerPos.y += _tileMap.tileSize.height;
} else {
playerPos.y -= _tileMap.tileSize.height;
}
}
- (void) dealloc
{
[super dealloc];
}
@end
Uilayer.h
コード:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "MainField.h"
#import "CCUIViewWrapper.h"
@interface Uilayer : CCLayer <UITextViewDelegate,MainFieldDelegate> { //ここのMainFieldDelegateでエラーが出ます
CCUIViewWrapper *uiWrapper;
UITextView *_textbox;
}
@end
Uilayer.m
コード:
#import "Uilayer.h"
#import "MainField.h"
@implementation Uilayer
-(id) init {
if ((self=[super init])) {
[self textbox];
}
-(void)textbox { //このビューにテキストを表示させてたい
_textbox = [[[UITextView alloc]init]autorelease];
_textbox.frame = CGRectMake(102, 6, 211, 75);
_textbox.backgroundColor = [UIColor whiteColor];
_textbox.textColor = [UIColor blackColor];
_textbox.font = [UIFont systemFontOfSize:10];
_textbox.editable = NO;
_textbox.textAlignment = NSTextAlignmentLeft;
uiWrapper = [CCUIViewWrapper wrapperForUIView:_textbox];
[self addChild:uiWrapper];
}
-(void)move_text { //テキストを表示させるメソッド
NSString *text_ = [NSString stringWithFormat:@"テキスト表示"];
_textbox.text =text_;
}
-(void)dealloc {
[super dealloc];
}
@end
Uilayer.hの<MainFieldDelegate>で“Cannot find protocol declaration for 'MainFieldDelegate"とエラーが出ます
web等で調べましたが良く理解できません。どうかご教授お願いいたします
Re: Delegateの記述の仕方が良く分かりません
Posted: 2013年1月20日(日) 19:51
by Graiai
それぞれのヘッダが、互いのヘッダをimportしているのが原因じゃないでしょうか。
MainField.hからUilayer.hのimportを削除するとか、import回りの整理してみて下さい。
Re: Delegateの記述の仕方が良く分かりません
Posted: 2013年1月22日(火) 20:22
by kira
お返事ありがとうございます
仰ったとおりMainfieldからUilayerのimportを抜きたり、またその逆を試してみましたが
やはり駄目でした。もう少し色々な方法をググってみます
Re: Delegateの記述の仕方が良く分かりません
Posted: 2013年1月26日(土) 00:09
by kira
“Cannot find protocol declaration for 'MainFieldDelegate"のエラーの原因が分かりました
Graiaiさんの仰るとおりimportでした。上のコードをUPする際、関係ないと思い記述しなかったimportの循環参照がエラーの
原因でした。しかし、エラーがなくなったものの目的であるdelegateがうまくいきません
Mainfield.h
コード:
#import "cocos2d.h"
#import "Title.h"
#import "Character.h"
@protocol MainFieldDelegate;
@interface MainField : CCLayer {
id <MainFieldDelegate> delegate;
}
-(id)init;
@property (nonatomic, assign) id <MainFieldDelegate> delegate;
@end
@protocol MainFieldDelegate <NSObject>
-(void)move_text:(MainField*)move_text text_:(NSString*)message;
@end
Mainfield.m
コード:
#import "MainField.h"
#import "Gamescene.h"
@implementation MainField
@synthesize delegate;
-(id) init {
if ((self=[super init])) {
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
return YES;
}
- (CGPoint)tileCoordForPosition:(CGPoint)position {
int x = position.x / _tileMap.tileSize.width;
int y = ((_tileMap.mapSize.height * _tileMap.tileSize.height) - position.y) / _tileMap.tileSize.height;
return ccp(x, y);
}
-(void)setPlayerPosition:(CGPoint)position {
CGPoint tileCoord = [self tileCoordForPosition:position];
int tileGid = [_meta tileGIDAt:tileCoord];
if (tileGid) {
NSDictionary *properties = [_tileMap propertiesForGID:tileGid];
if (properties) {
NSString *collision = [properties valueForKey:@"Collidable"];
if (collision && [collision compare:@"True"] == NSOrderedSame) {
return;
}
}
}
_player.position = position;
}
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
CGPoint playerPos = _player.position;
CGPoint diff = ccpSub(touchLocation, playerPos);
if (abs(diff.x) > abs(diff.y)) {
if (diff.x > 0) {
[delegate move_text:self text_:@"north"]; //画面タップでdelegateに通知
playerPos.x += _tileMap.tileSize.width;
} else {
playerPos.x -= _tileMap.tileSize.width;
}
} else {
if (diff.y > 0) {
playerPos.y += _tileMap.tileSize.height;
} else {
playerPos.y -= _tileMap.tileSize.height;
}
}
if (playerPos.x <= (_tileMap.mapSize.width * _tileMap.tileSize.width) &&
playerPos.y <= (_tileMap.mapSize.height * _tileMap.tileSize.height) &&
playerPos.y >= 0 &&
playerPos.x >= 0 )
{
[self setPlayerPosition:playerPos];
}
[self setViewpointCenter:_player.position];
}
- (void) dealloc
{
[super dealloc];
}
@end
Uilayer.h
コード:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "CCUIViewWrapper.h"
#import "MainField.h"
@interface Uilayer :CCLayer <UITextViewDelegate,MainFieldDelegate> {
CCUIViewWrapper *uiWrapper;
UITextView *_textbox;
}
@end
Uilayer.m
コード:
#import "Uilayer.h"
#import "Character.h"
@implementation Uilayer
-(id) init {
if ((self=[super init])) {
[self textbox];
MainField *_main = [[[MainField alloc]init]autorelease];
_main.delegate = self;
}
return self;
}
-(void)textbox {
_textbox = [[[UITextView alloc]init]autorelease];
_textbox.frame = CGRectMake(102, 6, 211, 75);
_textbox.backgroundColor = [UIColor whiteColor];
_textbox.textColor = [UIColor blackColor];
_textbox.font = [UIFont systemFontOfSize:10];
_textbox.editable = NO;
_textbox.textAlignment = NSTextAlignmentLeft;
uiWrapper = [CCUIViewWrapper wrapperForUIView:_textbox];
[self addChild:uiWrapper];
}
-(void)move_text:(MainField*)move_text text_:(NSString *)message {
_textbox.text = message;
NSLog(@"%@",message);
}
-(void)dealloc {
[super dealloc];
}
@end
Re: Delegateの記述の仕方が良く分かりません
Posted: 2013年1月26日(土) 02:19
by Graiai
コード:
MainField *_main = [[[MainField alloc]init]autorelease];
_main.delegate = self;
この直後 _mainは autorelease指定により破棄対象になってしまいませんか?
Re: Delegateの記述の仕方が良く分かりません
Posted: 2013年1月26日(土) 10:35
by kira
早速のご返信ありがとうございます
(Mainfield*)_mainのautoreleaseを削除してみましたがダメでした
Uilayer.m
コード:
#import "Uilayer.h"
#import "Character.h"
@implementation Uilayer
-(id) init {
if ((self=[super init])) {
[self textbox];
MainField *_main = [[MainField alloc]init];
_main.delegate = self;
-(void)textbox {
_textbox = [[UITextView alloc]init];
_textbox.frame = CGRectMake(102, 6, 211, 75);
_textbox.backgroundColor = [UIColor whiteColor];
_textbox.textColor = [UIColor blackColor];
_textbox.font = [UIFont systemFontOfSize:10];
_textbox.editable = NO;
_textbox.textAlignment = NSTextAlignmentLeft;
uiWrapper = [CCUIViewWrapper wrapperForUIView:_textbox];
[self addChild:uiWrapper];
}
-(void)move_text:(MainField*)move_text text_:(NSString *)message {
_textbox.text = message;
NSLog(@"%@",message);
}
-(void)dealloc {
[super dealloc];
}
@end
うーん難しいですね
Re: Delegateの記述の仕方が良く分かりません
Posted: 2013年1月26日(土) 14:17
by Graiai
あとはそうですね、問題の [delegate move_text:self text_:@"north"] の行まで
コードが到達していない可能性や下手すると ccTouchEnded にすらと到達していない可能性も考えられます。
それぞれにブレークポイントを貼るか、NSLog で出力するなどで通過しているか確認してみて下さい。
ところで CCLayerを継承したクラスって、シーンに追加しなくて良かったんでしたっけ?
Re: Delegateの記述の仕方が良く分かりません
Posted: 2013年1月26日(土) 16:37
by kira
すいません
見やすい様にと思い、余計な部分を省略してUPしていました
こちらのMainfieldはGamesceneという別なクラスのCCSceneに貼付けています。
Mainfield.m
コード:
#import "MainField.h"
#import "Gamescene.h"
@implementation MainField
@synthesize tileMap = _tileMap;
@synthesize background = _background;
@synthesize player = _player;
@synthesize meta = _meta;
@synthesize delegate;
-(id) init {
if ((self=[super init])) {
self.tileMap = [CCTMXTiledMap tiledMapWithTMXFile:@"IquestMap.tmx"];
self.background = [_tileMap layerNamed:@"Background"];
[self addChild:_tileMap];
self.meta = [_tileMap layerNamed:@"Meta"];
_meta.visible = NO;
CCTMXObjectGroup *objects = [_tileMap objectGroupNamed:@"Objects"];
NSAssert(objects != nil, @"'Objects' object group not found");
NSMutableDictionary *spawnPoint = [objects objectNamed:@"SpawnPoint"];
NSAssert(spawnPoint != nil, @"SpawnPoint object not found");
int x = [[spawnPoint valueForKey:@"x"] intValue];
int y = [[spawnPoint valueForKey:@"y"] intValue];
self.player = [CCSprite spriteWithFile:@"騎士.png"];
_player.position = ccp(x, y);
[self addChild:_player z:1];
self.isTouchEnabled = YES;
[self setViewpointCenter:_player.position];
}
return self;
}
-(void)setViewpointCenter:(CGPoint) position {
CGSize winSize = [[CCDirector sharedDirector] winSize];
int x = MAX(position.x, winSize.width / 2);
int y = MAX(position.y, winSize.height / 2);
x = MIN(x, (_tileMap.mapSize.width * _tileMap.tileSize.width)
- winSize.width / 2);
y = MIN(y, (_tileMap.mapSize.height * _tileMap.tileSize.height)
- winSize.height/2);
CGPoint actualPosition = ccp(x, y);
CGPoint centerOfView = ccp(winSize.width/2, winSize.height/2);
CGPoint viewPoint = ccpSub(centerOfView, actualPosition);
self.position = viewPoint;
}
-(void) registerWithTouchDispatcher {
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self
priority:0 swallowsTouches:YES];
}
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
return YES;
}
- (CGPoint)tileCoordForPosition:(CGPoint)position {
int x = position.x / _tileMap.tileSize.width;
int y = ((_tileMap.mapSize.height * _tileMap.tileSize.height) - position.y) / _tileMap.tileSize.height;
return ccp(x, y);
}
-(void)setPlayerPosition:(CGPoint)position {
CGPoint tileCoord = [self tileCoordForPosition:position];
int tileGid = [_meta tileGIDAt:tileCoord];
if (tileGid) {
NSDictionary *properties = [_tileMap propertiesForGID:tileGid];
if (properties) {
NSString *collision = [properties valueForKey:@"Collidable"];
if (collision && [collision compare:@"True"] == NSOrderedSame) {
return;
}
}
}
_player.position = position;
}
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
[delegate move_text:self text_:@"north"]; //画面タップ終了後にdelegate通知
CGPoint touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
CGPoint playerPos = _player.position;
CGPoint diff = ccpSub(touchLocation, playerPos);
if (abs(diff.x) > abs(diff.y)) {
if (diff.x > 0) {
playerPos.x += _tileMap.tileSize.width;
} else {
playerPos.x -= _tileMap.tileSize.width;
}
} else {
if (diff.y > 0) {
playerPos.y += _tileMap.tileSize.height;
} else {
playerPos.y -= _tileMap.tileSize.height;
}
}
if (playerPos.x <= (_tileMap.mapSize.width * _tileMap.tileSize.width) &&
playerPos.y <= (_tileMap.mapSize.height * _tileMap.tileSize.height) &&
playerPos.y >= 0 &&
playerPos.x >= 0 )
{
[self setPlayerPosition:playerPos];
}
[self setViewpointCenter:_player.position];
}
- (void) dealloc
{
self.tileMap = nil;
self.background = nil;
self.player = nil;
self.meta = nil;
[super dealloc];
}
@end
恥ずかしながらBreakpointの使い方が良くわからず、今webで調べています
Re: Delegateの記述の仕方が良く分かりません
Posted: 2013年1月26日(土) 16:57
by kira
Uilayer.m
コード:
-(void)textbox {
_textbox = [[UITextView alloc]init];
_textbox.frame = CGRectMake(102, 6, 211, 75);
_textbox.backgroundColor = [UIColor whiteColor];
_textbox.textColor = [UIColor blackColor];
_textbox.font = [UIFont systemFontOfSize:10];
_textbox.editable = NO;
_textbox.textAlignment = NSTextAlignmentLeft;
uiWrapper = [CCUIViewWrapper wrapperForUIView:_textbox];
[self addChild:uiWrapper];
}
-(void)move_text:(MainField*)move_text text_:(NSString *)message { //ここにBreakpointを設定
_textbox.text = message;
NSLog(@"%@",message);
}
上記の部分にBreakpointを設定しました
Mainfieldの画面タップにより上記のメソッドが動き、UITextviewにテキストが表示されれば目的達成なんですが
ビルドして画面をタップしてもプログラムがストップしません。ということはdelegateから指示が届いていないということでしょうか?
Mainfield.mのccTouchEndedにBreakpointを設定したら、画面タップによりプログラムは止まりましたがdelegateに通知しているかどうか
不慣れなもので良くわかりませんでした
Re: Delegateの記述の仕方が良く分かりません
Posted: 2013年1月26日(土) 19:40
by Graiai
kira さんが書きました:Mainfield.mのccTouchEndedにBreakpointを設定したら、画面タップによりプログラムは止まりました
ということは、新しく投稿されたコードから見るとccTouchEndedのブレークポイントで止まるということは
[delegate move_text:self text_:@"north"]の行は(メソッドが呼び出されているかどうかは別として)
実行されている、と考えてよさそうですね。
であれば、この行にブレークポイントを貼って、その時点の delegateの値が nilに
なっていないかどうか確認して見て下さい。
nilであれば、delegateが何らかの理由でセットされていないか、セットはされていたけど
後からnilに初期化されてしまっているのが原因だと思われます。
delegateがnilで無ければブレークポイントでプログラムが停止した状態から、"Step Into"(F7)を行って下さい。
どうなりますか?
Re: Delegateの記述の仕方が良く分かりません
Posted: 2013年1月26日(土) 20:24
by kira
丁寧なご回答ありがとうございます
delegateの値なんですが”delegate = (obj_object*) 0×00000000 isa(obj_class*)”と表示されています
Step infoで、その一つ下の行に移動しました
Re: Delegateの記述の仕方が良く分かりません
Posted: 2013年1月26日(土) 21:39
by Graiai
kira さんが書きました:delegateの値なんですが”delegate = (obj_object*) 0×00000000 isa(obj_class*)”と表示されています
Step infoで、その一つ下の行に移動しました
MainField#delegateが nilになっている、ということですね。
delegateが nilでは呼び出しは行われません。
可能性としては2つ。
MainField#delegateプロパティにUilayerオブジェクトをセットしていないか、
セットはしたけど、何かの拍子にどこかで nilを代入してしまった、のどちらかとなります。
そのあたりを疑って、問題の箇所を探してみて下さい。
Re: Delegateの記述の仕方が良く分かりません
Posted: 2013年1月27日(日) 20:30
by kira
お世話になります
いろいろと調べてみたのですが・・・
Graiaiさん さんが書きました:
可能性としては2つ。
MainField#delegateプロパティにUilayerオブジェクトをセットしていないか、
セットはしたけど、何かの拍子にどこかで nilを代入してしまった、のどちらかとなります。
すいません、初歩的な事ですが具体的な記述が思いつきません
initメソッド内で ”Uilayer *uilayer = [Uilater alloc]init];
[uilayer setDelegate:self];"
とやってみましたが・・・。 当初の様に循環参照になってしまいエラーになってしまいます
もしかしたらUITextviewはテキストの更新ができないのかと思い、CCLabelでやってみましたがやはり結果を一緒でした
Re: Delegateの記述の仕方が良く分かりません
Posted: 2013年1月27日(日) 21:46
by Graiai
Graiaiさん さんが書きました:”Uilayer *uilayer = [Uilater alloc]init];
[uilayer setDelegate:self];"
じゃなくて、通知元は MainFieldなのですから、
コード:
MainField *mf = [[MainField alloc]init];
mf.delegate = uilayer;
ですよね。
Re: Delegateの記述の仕方が良く分かりません
Posted: 2013年1月27日(日) 22:30
by kira
こんばんわ
解決いたしました!!!!
原因は2つでした(興奮気味)
1 UilayerでMainfieldのインスタンスを作成するまでは良かったのですがaddChild(Uilayerの子にして反映していなかった)
2 途中からUITextview → CCLabelに切り替えたのですが、Mainfieldクラスからdelegateにより通知された文字列の取得の際
CCLabelへのテキストの更新要領が間違っていた
です。
Mainfield.h
コード:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "Title.h"
@protocol MainfieldDelegate;
@interface MainField : CCLayer {
CCTMXTiledMap *_tileMap;
CCTMXLayer *_background;
CCSprite *_player;
CCTMXLayer *_meta;
id <MainfieldDelegate>delegate;
}
-(id)init;
-(void)setViewpointCenter:(CGPoint)position;
@property (nonatomic, retain) CCTMXTiledMap *tileMap;
@property (nonatomic, retain) CCTMXLayer *background;
@property (nonatomic, retain) CCSprite *player;
@property (nonatomic, retain) CCTMXLayer *meta;
@property (nonatomic, retain) id<MainfieldDelegate>delegate;
@end
@protocol MainfieldDelegate
-(void)move_text:(NSString*)message;
@end
Mainfield.m
コード:
#import "MainField.h"
#import "Gamescene.h"
#import "Uilayer.h"
@implementation MainField
@synthesize tileMap = _tileMap;
@synthesize background = _background;
@synthesize player = _player;
@synthesize meta = _meta;
@synthesize delegate;
-(id) init {
if ((self=[super init])) {
self.tileMap = [CCTMXTiledMap tiledMapWithTMXFile:@"IquestMap.tmx"];
self.background = [_tileMap layerNamed:@"Background"];
[self addChild:_tileMap];
self.meta = [_tileMap layerNamed:@"Meta"];
_meta.visible = NO;
CCTMXObjectGroup *objects = [_tileMap objectGroupNamed:@"Objects"];
NSAssert(objects != nil, @"'Objects' object group not found");
NSMutableDictionary *spawnPoint = [objects objectNamed:@"SpawnPoint"];
NSAssert(spawnPoint != nil, @"SpawnPoint object not found");
int x = [[spawnPoint valueForKey:@"x"] intValue];
int y = [[spawnPoint valueForKey:@"y"] intValue];
self.player = [CCSprite spriteWithFile:@"騎士.png"];
_player.position = ccp(x, y);
[self addChild:_player z:1];
self.isTouchEnabled = YES;
[self setViewpointCenter:_player.position];
}
return self;
}
-(void)setViewpointCenter:(CGPoint) position {
CGSize winSize = [[CCDirector sharedDirector] winSize];
int x = MAX(position.x, winSize.width / 2);
int y = MAX(position.y, winSize.height / 2);
x = MIN(x, (_tileMap.mapSize.width * _tileMap.tileSize.width)
- winSize.width / 2);
y = MIN(y, (_tileMap.mapSize.height * _tileMap.tileSize.height)
- winSize.height/2);
CGPoint actualPosition = ccp(x, y);
CGPoint centerOfView = ccp(winSize.width/2, winSize.height/2);
CGPoint viewPoint = ccpSub(centerOfView, actualPosition);
self.position = viewPoint;
}
-(void) registerWithTouchDispatcher {
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self
priority:0 swallowsTouches:YES];
}
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
return YES;
}
- (CGPoint)tileCoordForPosition:(CGPoint)position {
int x = position.x / _tileMap.tileSize.width;
int y = ((_tileMap.mapSize.height * _tileMap.tileSize.height) - position.y) / _tileMap.tileSize.height;
return ccp(x, y);
}
-(void)setPlayerPosition:(CGPoint)position {
CGPoint tileCoord = [self tileCoordForPosition:position];
int tileGid = [_meta tileGIDAt:tileCoord];
if (tileGid) {
NSDictionary *properties = [_tileMap propertiesForGID:tileGid];
if (properties) {
NSString *collision = [properties valueForKey:@"Collidable"];
if (collision && [collision compare:@"True"] == NSOrderedSame) {
return;
}
}
}
_player.position = position;
}
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
CGPoint playerPos = _player.position;
CGPoint diff = ccpSub(touchLocation, playerPos);
if (abs(diff.x) > abs(diff.y)) {
if (diff.x > 0) {
[delegate move_text:@"north"]; //delegateに通知
playerPos.x += _tileMap.tileSize.width;
} else {
playerPos.x -= _tileMap.tileSize.width;
}
} else {
if (diff.y > 0) {
playerPos.y += _tileMap.tileSize.height;
} else {
playerPos.y -= _tileMap.tileSize.height;
}
}
if (playerPos.x <= (_tileMap.mapSize.width * _tileMap.tileSize.width) &&
playerPos.y <= (_tileMap.mapSize.height * _tileMap.tileSize.height) &&
playerPos.y >= 0 &&
playerPos.x >= 0 )
{
[self setPlayerPosition:playerPos];
}
[self setViewpointCenter:_player.position];
}
- (void) dealloc
{
self.tileMap = nil;
self.background = nil;
self.player = nil;
self.meta = nil;
[super dealloc];
}
@end
Uilayer.h
コード:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "CCUIViewWrapper.h"
#import "MainField.h"
@interface Uilayer :CCLayer <UITextViewDelegate,MainfieldDelegate> {
CCUIViewWrapper *uiWrapper;
CCLabelTTF *_lebel;
MainField *_main;
}
@end
Mainfield.m
コード:
#import "Uilayer.h"
#import "Character.h"
#import "MainField.h"
CCLabelTTF *_textbox;
CCUIViewWrapper *uiWrapper;
NSMutableString *talk_text;
@implementation Uilayer
@synthesize mainframe = _mainframe;
-(id) init {
if ((self=[super init])) {
self.mainframe = [CCSprite spriteWithFile:@"mainframe-320x480.png"];
_mainframe.anchorPoint = ccp(0, 0);
[self addChild:self.mainframe z:1];
_main = [[MainField alloc]init]; //Mainfieldのインスタンス作成
_main.delegate = self;
[self addChild:_main]; //原因その1 初歩的ミス
talk_text = [[NSMutableString alloc]initWithFormat:@""]; //途中からUITextviewからこっちに切り替えました
CCLabelTTF *_textbox = [CCLabelTTF labelWithString:talk_text fontName:@"Times New Roman" fontSize:10];
_textbox.position = CGPointMake(155, 465);
_textbox.color = ccc3(255,255,255);
[self addChild:_textbox z:1 tag:1]; //原因その2 このLabelを取得するため"tag"を設定
}
return self;
}
/*-(void)textbox { //途中から使うのをやめました
_textbox = [[UITextview alloc]init];
_textbox.frame = CGRectMake(102, 6, 211, 75);
_textbox.backgroundColor = [UIColor whiteColor];
_textbox.textColor = [UIColor blackColor];
_textbox.font = [UIFont systemFontOfSize:10];
_textbox.textAlignment = NSTextAlignmentLeft;
uiWrapper = [CCUIViewWrapper wrapperForUIView:_textbox];
[self addChild:uiWrapper z:1 tag:2];
}*/
-(void)move_text:(NSString*)message { //delegateからの通知で動くメソッド
NSLog(@"movetext");
talk_text = [NSMutableString stringWithString:message];
_textbox = (CCLabelTTF*)[self getChildByTag:1]; //tagを指定してCCLabel *talk_textを取得
[_textbox setString:[NSMutableString stringWithString:talk_text]]; //Labelにテキストをセット
}
-(void)dealloc {
[super dealloc];
}
@end
基本的な知識に欠けていると、思わぬ所でつまずくと痛感いたしました
CCLabelのテキストをグローバル変数に代入させていたのですが、なぜかうまくいかずにget tag(cocos2dの書籍で知った)を使ったらうまくいきました。
Graiaiさんには親身に相談に乗っていただき、本当に本当に感謝しております。
ありがとうございました