我试图理解在iphone游戏开发中使用像lua这样的脚本语言的好处(例如使用cocos2d),如果它可以帮助解决我的问题(并提高我的编码技能).在我的游戏中,我有以下代码:
-(void)MenuItem:(CCMenuItem *) menuItem {
switch (menuItem.tag) {
case 1:
[[CCDirector sharedDirector] replaceScene:[Level1 scene]];
break;
case 2:
[[CCDirector sharedDirector] replaceScene:[Level2 scene]];
break;
case 3:
[[CCDirector sharedDirector] replaceScene:[Level3 scene]];
break;
case 4:
[[CCDirector sharedDirector] replaceScene:[Level4 scene]];
break;
case 5:
[[CCDirector sharedDirector] replaceScene:[Level5 scene]];
break;
case 6:
[[CCDirector sharedDirector] replaceScene:[Level6 scene]];
break;
case 7:
[[CCDirector sharedDirector] replaceScene:[Level7 scene]];
break;
case 8:
[[CCDirector sharedDirector] replaceScene:[Level8 scene]];
break;
default:
break;
}
Run Code Online (Sandbox Code Playgroud)
该函数的问题是如果我有50个级别,这个函数将需要3页代码.我想用以下代码替换整个函数:
-(void)MenuItem:(CCMenuItem *) menuItem {
[[CCDirector sharedDirector] replaceScene:[<script> @"Level" + menuItem.tag</script> scene]];
} …Run Code Online (Sandbox Code Playgroud) 我希望能够为CCSpriteBatchNode标识一组对象,但还要标识该组的子组.做类似的事情
CCArray *listOfGameObjects = [sceneSpriteBatchNode children];
for (GameObject *tempObject in listOfGameObjects) {
if ([tempObject tag] == kBottleTagValue) {
//make bottle yellow
if ([tempObject tag] == kBrokenBottleTagValue) {
//also make bottle smaller
}
}
}
Run Code Online (Sandbox Code Playgroud)
在该示例中,所有瓶子都将变成黄色,并且如果瓶子也被标记为破碎,则它将变得更小.因此需要使用kBottleTagValue和kBrokenBottleTagValue标记损坏的瓶子.有没有办法做到这一点?因为当我尝试添加两个标签时它失败了.
我为什么要来
cocos2d:CCFileUtils:找不到警告HD文件:META-hd.png
如果我的项目中肯定有META-hd.png文件?
我正在做的是运行我的.tmx tilemap.该地图使用一个搜索"META.png"的tileset(没有-hd后缀,因为我希望cocos2d自动将其打开).
我正在为iPhone/iPodTouch编写游戏(使用Cocos2d),我发现了一些奇怪的东西......
我需要检测播放器触摸的屏幕部分以执行某些操作.
例如:
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [touch locationInView:[touch view]];
//DEBUG
CCLOG(@"Button pressed: X Location: %f",touchLocation.x);
CCLOG(@"Button pressed: Y Location: %f",touchLocation.y);
Run Code Online (Sandbox Code Playgroud)
...当touchLocation.x和touchLocation.y都在一定范围内时,我会做一些事情.
它在模拟器上工作得很好.当我为我的测试设备编译它时,使用armv7架构(优化)它也有效.但是当我使用armv6编译它时,启用了armv7架构(这是应用商店需要的),我注意到代码不再有效.CCLOG向我展示了我为armv6编译时使用的X,Y坐标系统,armv7与我仅为armv7(优化)编译时所使用的不同.
我怎样才能解决这个问题?是在设备上测试时使用armv6,armv7使用的坐标系的唯一方法......(这在模拟器上不起作用)?我无法上传iPhone的应用程序,只能编译为armv7(优化).:(
有没有办法让我检查滑动手势是否通过屏幕上的特定点?
我想要实现这样的事情:
http://screenshots.en.sftcdn.net/en/scrn/3337000/3337188/dot-lock-03-356x535.jpg
如果跟踪滑动的方法不适合实现这一点,请告诉我.
谢谢.
如何在GameConfig.h中存储CCLabelTTF颜色,如:
#define kGameLabelColor ccc3(79,71,59);
Run Code Online (Sandbox Code Playgroud) 我正在开发一款游戏,其中我试图点击两种不同的类型,如果分别点击单击和双击.
继承人我在触摸开始的方法:
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for( UITouch *touch in touches )
{
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL: location];
NSLog(@"TOUCH LOCATION IN TOUCH BEGAN = (%f , %f)", location.x , location.y);
NSUInteger tapCount = [touch tapCount];
switch (tapCount)
{
case 1:
{
NSDictionary * touchloc = [NSDictionary dictionaryWithObject:[NSValue valueWithCGPoint:location] forKey:@"location"];
[self performSelector:@selector(startTimer:) withObject:touchloc afterDelay:3];
break;
}
case 2:
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startTimer) object:nil];
[self performSelector:@selector(removeBall) withObject:nil afterDelay:1];
break;
}
default:
{ …Run Code Online (Sandbox Code Playgroud) 我正为我的游戏设置音效音量.我希望音量从0.0增加到1.0,增量为0.1.
但是,当它达到零时,我的日志反而告诉我它是-0.00000并继续倒计时.请告诉我有什么问题.谢谢
-(void)sfxUp{
NSLog(@"SFX UP %f",[[NSUserDefaults standardUserDefaults]floatForKey:@"sfx_volume"]);
if ([[NSUserDefaults standardUserDefaults]floatForKey:@"sfx_volume"] == 1.0f)
return;
[[NSUserDefaults standardUserDefaults]setFloat:([[NSUserDefaults standardUserDefaults]floatForKey:@"sfx_volume"]+0.1f) forKey:@"sfx_volume"];
[[NSUserDefaults standardUserDefaults]synchronize];
[[SimpleAudioEngine sharedEngine] setEffectsVolume:[[NSUserDefaults standardUserDefaults]floatForKey:@"sfx_volume"]];
NSLog(@"SFX UP %f",[[NSUserDefaults standardUserDefaults]floatForKey:@"sfx_volume"]);
}
-(void)sfxDown{
NSLog(@"SFX DOWN %f",[[NSUserDefaults standardUserDefaults]floatForKey:@"sfx_volume"]);
if ([[NSUserDefaults standardUserDefaults]floatForKey:@"sfx_volume"] == 0.0f)
return;
[[NSUserDefaults standardUserDefaults]setFloat:([[NSUserDefaults standardUserDefaults]floatForKey:@"sfx_volume"]-0.1f) forKey:@"sfx_volume"];
[[NSUserDefaults standardUserDefaults]synchronize];
[[SimpleAudioEngine sharedEngine] setEffectsVolume:[[NSUserDefaults standardUserDefaults]floatForKey:@"sfx_volume"]];
NSLog(@"SFX DOWN %f",[[NSUserDefaults standardUserDefaults]floatForKey:@"sfx_volume"]);
}
Run Code Online (Sandbox Code Playgroud)
单击"降低音量"按钮,这是我的日志输出:
2012-06-13 18:32:25.067 Bomb Defuse[80558:1c403] SFX DOWN 1.000000
2012-06-13 18:32:25.068 Bomb Defuse[80558:1c403] SFX DOWN 0.900000
2012-06-13 18:32:25.385 Bomb Defuse[80558:1c403] SFX DOWN 0.900000
2012-06-13 18:32:25.386 Bomb Defuse[80558:1c403] …Run Code Online (Sandbox Code Playgroud) 我有一个有多个实例变量的类.我想在课堂上达到两个目的.我可能只将某些变量用于一个目的,有时可以同时使用这两个变量.
这是一个更具体的例子.我想创建一个类,每次用户点击屏幕时,狗精灵和猫精灵都会出现动画.如果再次点击,他们将继续执行不同的动画.但是,有时我只想让狗精灵出现并更新.还有一些其他罕见的时刻,我希望猫精灵在狗精灵出现之后出现几次.
问题是:实例变量是否分配了太多内存?我非常关心性能,因为我打算制作内存密集型游戏.由于我很难预测何时实际使用所有实例变量,我应该将它们分成两个类吗?让我们划分可能的场景以获得更好的想法.
只使用Dog Sprite并且cat sprite永远不会出现:如果留在一个类中,则cat的实例变量保持不变.
狗精灵首先出现,然后猫精灵出现:两个精灵最终都会出现.可以将它分成两个类,但是由于诸如触摸提前逻辑和动画之类的方法是相似的,因此一些方法是重复的.但是如果我们把它留在一次,那么可能会出现情景1,这可能会在没有重复复制很多代码的情况下解决.
其他事情可能会发生,但上面已经讨论过这些问题.从我的角度来看,这些是赞成和反对的:
一类方法
无需导入导致某些类似实例变量的多个标头
精读
两类方法
可能从类继承,而不会在将来继承一些不必要的变量
精读
我很难决定应该使用哪个选项.任何建议都会很棒!先感谢您!
if (didHelp)
for (int x = 0; x < 100; x++)
NSLog(@"Thanks!");
Run Code Online (Sandbox Code Playgroud) 我怎么能通过
-(void)explosionFromPoint:(CGPoint)explosionPoint withSprite:(CCSprite*)sprite;
在一个
[self performSelector:@selector(//Right here) withObject:nil afterDelay:3];?
你不能把整个选择器放在里面@selector(),withObject首先只允许一个对象被传递,我也不理解如何使用它.
如何在延迟后传递带有对象的方法?我也尝试了一个解决方法,我打电话
[self performSelector:@selector(waitExplosion) withObject:nil afterDelay:3];
然后运行动作本身,[self explosionFromPoint:c0TileCoord withSprite:bomb];
但这是一个非常糟糕的方法,因为我必须重新声明变量,它只是坏.
如何在延迟后传递带有对象的方法?
cocos2d-iphone ×10
ios ×6
iphone ×6
objective-c ×6
xcode ×2
armv6 ×1
armv7 ×1
cgpoint ×1
cocoa-touch ×1
label ×1
nsdictionary ×1
performance ×1
scripting ×1