在iPhone上加载包含游戏级别地图的静态数据文件的最佳方法是什么?我已经看到一些使用XML文件,但在我的情况下这似乎有点过分,因为数据文件只是决定了游戏级别的布局,即障碍物的位置.另外,它们应该存储在哪个项目目录中?
提前致谢!
我正在尝试更改文件
GameConfig.h:
#define GAME_AUTOROTATION kGameAutorotationNone
Run Code Online (Sandbox Code Playgroud)
和App代表:
//#if GAME_AUTOROTATION == kGameAutorotationUIViewController
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
//#else
//[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
//#endif
Run Code Online (Sandbox Code Playgroud)
应用程序转为纵向模式,但Box2d机构强制重力向右
我一直在使用以下方法来检测触摸何时开始以及位置是什么:
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
CGPoint touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
return YES;
}
Run Code Online (Sandbox Code Playgroud)
但我还想知道如果我的手指仍然在屏幕上,我可以随时获取当前位置.谁知道怎么样?谢谢.
编辑
我会稍微发布这个作为答案,但这就是我所做的:
我只是在我所有方法之外定义的ccTime方法中转换变量:
- (void)update:(ccTime)dt {
heroMoveEndLoc = [self convertToNodeSpace:heroMoveEndLoc];
}
Run Code Online (Sandbox Code Playgroud) 如果用户在特定时间内没有触摸屏幕,我必须执行特定操作.我怎么能在Cocos2d中做到这一点?
为什么如果我试图在cocos2d中调用动画函数,如下所示:
-(id) init
{
if( (self=[super init] ) )
{
[self animation];
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
它工作得很好,但是当我这样做时:
-(id) init
{
[self animation];
if( (self=[super init] ) )
{
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
动画被调用但没有被播放?或者我看不到?我只是无法理解.
我在cocos2D中使用一个调用方法并将BOOL作为参数传递的动作.
我收到警告:"传递参数3'actionWithTarget:selector:data:'使用此行生成没有强制转换的整数的指针":
id actionCharacterReaction = [CCCallFuncND actionWithTarget:self selector:@selector(characterReaction : data:) data:flipChar];
Run Code Online (Sandbox Code Playgroud)
我试过了:
id actionCharacterReaction = [CCCallFuncND actionWithTarget:self selector:@selector(characterReaction : data:) data:(BOOL)flipChar];
Run Code Online (Sandbox Code Playgroud)
我的方法看起来像这样:
-(void) characterReaction:(id)sender data:(BOOL)flipChar {
*code stuff inside*
}
Run Code Online (Sandbox Code Playgroud)
它似乎仍然很好.我对这个警告感到恼火.有任何想法吗?
使用动作调用方法的适当方法是什么,以及传递CGPoint参数的方法本身应该是什么样的?我试图在网上查找没有太多运气的例子,所以我一直在猜测.
我试过的就是这个叫它:
CGPoint spriteCoord = saveStation.sprite.position;
id a1=[CCMoveTo actionWithDuration:.4 position:ccp(saveStation.sprite.position.x,saveStation.sprite.position.y)];
id actionSaveStationReaction = [CCCallFuncND actionWithTarget:self selector:@selector(saveStationReaction : data:) data:&spriteCoord];
[hero.heroSprite runAction:[CCSequence actions:a1, actionSaveStationReaction, nil]];
Run Code Online (Sandbox Code Playgroud)
方法本身:
-(void) saveStationReaction:(id)sender data:(void *)data {
CGPoint spriteCoord = (void *)data; //error: Invalid initializer
NSLog(@"spriteCoord x = %f", spriteCoord.x);
NSLog(@"spriteCoord y = %f", spriteCoord.y);
}
Run Code Online (Sandbox Code Playgroud) 我目前是目标c的新手,在制作游戏时遇到了一个问题我有一个名为battleEngine的自定义对象,它是我在cocos2d的helloWorld场景中的一个实例变量.该对象有一个对象作为一个名为plyController的实例变量,它是一个PlayerController对象.我希望battleEngine有一个getter方法返回plyController对象,这段代码不起作用:
-(PlayerController*)getPlayerController
{
return plyController;
}
Run Code Online (Sandbox Code Playgroud) 我正在cocos2d中制作一个简单的物理游戏,并希望在滑动速度的同时启动一个粒子.为了获得速度,我需要进行两次触摸并确定(位置差异)/(时间戳的差异).我的问题是我无法接触两次.我尝试了几种方法,但没有一种方法可行.
我尝试存储第一次触摸
@property (nonatomic, strong) UITouch *firstTouch;
...
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
[self setFirstTouch:touch];
NSLog(@"first touch time 1: %f", self.firstTouch.timestamp);
}
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
NSLog(@"touch ended");
NSLog(@"first touch time 2: %f", self.firstTouch.timestamp);
NSLog(@"end of touch time: %f", touch.timestamp);
}
Run Code Online (Sandbox Code Playgroud)
但这给了我每次时间戳0的差异.它似乎用最近的触摸取代了firstTouch
我是否做错了指针,这被替换了?
也许我可以从ccTouchesMoved那里接受最后两次接触?
我想将CCSprite转换为UIImage.我写了一个简单的测试项目.但是,我发现它不起作用.尽管图像尺寸合适,但UIImage无法在屏幕上显示.你能告诉我原因吗?
测试项目的URL如下:https://github.com/HelloVicent/ConverCCSpriteTOUIImage
非常感谢.
cocos2d-iphone ×10
objective-c ×6
iphone ×5
ios ×4
methods ×3
cocoa-touch ×2
touch ×2
arguments ×1
box2d ×1
box2d-iphone ×1
ccsprite ×1
cgpoint ×1
getter ×1
parameters ×1
selector ×1
uiimage ×1
xcode ×1