こんばんわ
解決いたしました!!!!
原因は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さんには親身に相談に乗っていただき、本当に本当に感謝しております。
ありがとうございました