我正在尝试在cocos2d中编写一个基本测试"游戏状态"单例,但由于某些原因加载应用程序时,从不调用initWithCoder.任何帮助将不胜感激,谢谢.
这是我的单身GameState.h:
#import "cocos2d.h"
@interface GameState : NSObject <NSCoding>
{
NSInteger level, score;
Boolean seenInstructions;
}
@property (readwrite) NSInteger level;
@property (readwrite) NSInteger score;
@property (readwrite) Boolean seenInstructions;
+(GameState *) sharedState;
+(void) loadState;
+(void) saveState;
@end
Run Code Online (Sandbox Code Playgroud)
...和GameState.m:
#import "GameState.h"
#import "Constants.h"
@implementation GameState
static GameState *sharedState = nil;
@synthesize level, score, seenInstructions;
-(void)dealloc {
[super dealloc];
}
-(id)init {
if(!(self = [super init]))
return nil;
level = 1;
score = 0;
seenInstructions = NO;
return self;
}
+(void)loadState {
@synchronized([GameState …Run Code Online (Sandbox Code Playgroud) singleton nskeyedarchiver cocos2d-iphone nsdata iphone-sdk-3.0
我正在使用chipmunk创建一个静态形状(使用SpaceManager)并将cpCCSprite附加到它上面.
但是,我需要精灵的锚点偏离中心,但是当我改变精灵的锚点时,形状和精灵不再对齐.
所以,如果我像这样改变锚点
[sprite setAnchorPoint:ccp(0.5, 0.3)];
Run Code Online (Sandbox Code Playgroud)
相应地绘制了精灵,但我预计形状会随之"移动".这是我的意思的图片.
大炮应该如何看待http://www.tomelders.com/bin/cannon.png
在左边是形状和精灵正确对齐.我没有改变锚点.
右边是具有锚点的精灵 ccp(0.5, 0.3)
我也在每一帧都重复静态形状.
这是它的创建方式
// create the sensor
sensor = [spaceMgr addRectAt:pPoint mass:STATIC_MASS width:53 height:81 rotation:0];
sensor->sensor = YES;
sensor->collision_type = 2;
//Create the sprite
CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage:@"bownce-sprites-comic-sized.png"];
barrel = [[cpCCSprite node] initWithShape:sensor texture:texture rect:CGRectMake(3, 428, 53, 82)];
[self addChild:barrel];
// set the ancor point
[barrel setAnchorPoint:ccp(0.5, 0.3)];
[barrel setPosition:pPoint];
Run Code Online (Sandbox Code Playgroud) 当我改变场景时,此代码会创建一个随机转换.有十一个过渡.因为它们是静态的,我想我不能使用NSClassFromString.我还需要传递场景对象.现在,转换似乎是多余的.有没有办法重写这个以提高效率?
-(void) newScene
{
CCScene *s = [CCScene node];
id child = [sceneCharacter node];
[s addChild:child];
int random = arc4random() % 10;
switch (random)
{
case 1:
[[CCDirector sharedDirector] replaceScene:[CCFadeTransition transitionWithDuration:0.5f scene:s]];
break;
case 2:
[[CCDirector sharedDirector] replaceScene:[CCFadeTRTransition transitionWithDuration:0.5f scene:s]];
break;
case 3:
[[CCDirector sharedDirector] replaceScene:[CCJumpZoomTransition transitionWithDuration:0.5f scene:s]];
break;
case 4:
[[CCDirector sharedDirector] replaceScene:[CCMoveInLTransition transitionWithDuration:0.5f scene:s]];
break;
case 5:
[[CCDirector sharedDirector] replaceScene:[CCOrientedTransitionScene transitionWithDuration:0.5f scene:s]];
break;
case 6:
[[CCDirector sharedDirector] replaceScene:[CCPageTurnTransition transitionWithDuration:0.5f scene:s]];
break;
case 7:
[[CCDirector sharedDirector] replaceScene:[CCRotoZoomTransition transitionWithDuration:0.5f …Run Code Online (Sandbox Code Playgroud) 我在我的Cocos2d游戏中使用GameKitHelper.在某些时候,我会想要显示排行榜,所以我这样做:
[[GameKitHelper sharedGameKitHelper] showLeaderboard];
Run Code Online (Sandbox Code Playgroud)
但没有任何反应.我该怎么办?
如何使用cocos2d发布纹理图集?
我有以下代码
NSString *blue= = @"Blue.plist";
CCSpriteBatchNode *blueBatchNode = [CCSpriteBatchNode batchNodeWithFile:@"Blue.png"];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:blue];
[self.parentScene addChild:blueBatchNode];
Run Code Online (Sandbox Code Playgroud)
如何在光栅阶段发布这两个?
在过去,我总是看到财产被称为position并且positionInPixels是相同的.这一次position * 2 == positionInPixels.谁能告诉我这两个属性之间有什么区别,或者它们的价值会有什么不同?
我有一个CClayer类,当这个类进入它时会创建一个应该居中的CCSprite,所以稍后,当我旋转用该CCLayer类创建的对象时,它会围绕它的中心旋转.我的意思是,如果该类的精灵是200像素宽和300像素高的图像,我希望CCLayer枢轴为100,150.
我试图将其设置为0,0和0.5,0.5但没有成功.
据我了解,CCLayer没有边界框,它就像一种节点,对吧?所以,我创建这样的类:
-(id) initWithImage:(UIImage*)image Name:(NSString*)name
{
if( (self=[super init])) {
self.isTouchEnabled = YES;
self.mySprite =
[CCSprite spriteWithCGImage:image.CGImage key:name];
self.mySprite.position = CGPointZero;
[self addChild:self.mySprite];
self.mySprite.anchorPoint = ccp(0.0f, 0.0f);
// have tried also 0.5f, 0.5f... no success
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
我怎么做?
谢谢
请告诉我如何在Sprite完成动画后获得一个事件?我正在使用cocos2d框架工作,
我的代码的某些部分是这样的..
-(void)playerTouchHurdlesOnFlore:(LHContactInfo*)contact
{
if(!isHurdlesAnimStart)
{
isHurdlesAnimStart=YES;
onGround=NO;
[player stopAnimation];
//[player setIsRelativeAnchorPoint:NO];
// I want to call any event or function after end of the this "Fall_Down" animation.
[player startAnimationNamed:@"Fall_Down"];
jumpCounter = 0.3f;
player.body->ApplyLinearImpulse(b2Vec2(0,2.0f), player.body->GetPosition());
}
}
Run Code Online (Sandbox Code Playgroud)
如果有的话,请告诉我任何解决方案或给我一些想法,
提前致谢 ,
:)
我目前正在尝试在cocos2d v2图层上永久显示TextField.
我正在使用这个CCTextField,因为它看起来很容易使用:https://github.com/iNinja/CCTextField
这个插件对于cocos2d v2有点旧,所以我更新了已弃用的方法,但我仍然无法显示任何文本字段...
我怎么想用cocos2d v2显示一个文本字段(任何方法都可以,只要它足够清晰且易于使用).
顺便说一句,我看了谷歌,并在stackoverflow和论坛上得到了2-3个答案.但是,它们适用于cocos2d v1(或更早版本),并且根本不起作用(或者不够清晰).
编辑
这就是我在班级的onEnter函数中所拥有的
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(200, 200, 150, 50)];
tf.delegate = self;
tf.textAlignment = UITextAlignmentLeft;
[[[CCDirector sharedDirector] view] addSubview:tf];
Run Code Online (Sandbox Code Playgroud)
这就是.h
@interface AdventureLayer : CCLayer <UITextFieldDelegate>
Run Code Online (Sandbox Code Playgroud)
什么都没有出现......
编辑2
哈哈哈!实际上TextField是不可见的!因此占位符非常有用
tf.placeholder = @"FOOBAR";
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下代码通过menuitem传递NSString
CCMenuItem * buyButton = [CCMenuItemLabel itemWithLabel:buyLabel target:self selector:@selector(buyItem:)];
buyButton.userData = (__bridge void *)((NSString*)(itemName));
Run Code Online (Sandbox Code Playgroud)
到以下选择器
-(void) buyItem:(CCMenuItemLabel*)sender {
NSString * itemName = (NSString *)sender.userData;
}
Run Code Online (Sandbox Code Playgroud)
但我在选择器中崩溃了.我正在使用启用了arc的cocos2d,因此是userdata分配中的桥接器.(kobold2d).有任何想法吗?
cocos2d-iphone ×10
objective-c ×4
ios ×3
iphone ×3
animation ×1
box2d ×1
chipmunk ×1
gamekit ×1
kobold2d ×1
nsdata ×1
singleton ×1
uitextfield ×1