因此,在对这个问题连续数月感到非常沮丧之后休息了一会儿,我现在回来尝试一劳永逸地解决我的问题。
我不打算发布代码,因为我拥有的代码是杂乱无章的,并且来自无数的观点。
我的问题是这样的:
我有一个精灵,他的信息存储在一个名为 player.m 的类中。我有一个游戏关卡,它存储在 GameLevelLayer.m
我让 player.m 存储速度、方向和有关角色的所有信息,然后我让 GameLevelLayer.m 实现、移动、使用边界检查碰撞等。
我现在想使用 spritesheet 使角色移动(我知道如何进行 spritesheeting,我只是不知道如何在使用两个不同的类时做到这一点)
我的问题是,我是否在 player.m 中创建批处理节点和 spritesheet 信息(CCBatchnodes、缓存等)以及所有操作和所有内容,然后在 GameLevelLayer.m 中运行它们?还是我在 GameLevelLayer.m 中创建所有这些
我真的需要一些帮助,因为我已经坚持了几个月
编辑:
多亏了社区成员的建议,这就是我所在的位置。
这是我的整个 player.m (长话短说,我定义了有关玩家的所有内容“您可能只想关注 init,因为这似乎是问题所在。其他的东西是我的物理引擎”)
#import "Player.h"
#import "SimpleAudioEngine.h"
#import "GameLevelLayer.h"
@implementation Player
@synthesize velocity = _velocity;
@synthesize desiredPosition = _desiredPosition;
@synthesize onGround = _onGround;
@synthesize forwardMarch = _forwardMarch, mightAsWellJump = _mightAsWellJump, isGoingLeft = _isGoingLeft;
@synthesize WalkAction = _WalkAction;
@synthesize isMoving = _isMoving;
-(id)initWithTexture:(CCTexture2D *)texture{
if (self = [super init]) {
self.velocity = ccp(0.0, 0.0);
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: @"BankerSpriteSheet_default.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"BankerSpriteSheet_default.png"];
[self addChild:spriteSheet];
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <=6; ++i) {
[walkAnimFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"banker%d.png", i]]];
CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.1f];
self = [super initWithSpriteFrameName:@"banker1.png"];
self.WalkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
//[_Banker runAction:_WalkAction];
[spriteSheet addChild:self];
[[CCAnimationCache sharedAnimationCache] addAnimation:walkAnim name:@"walkAnim"];
}
}
return self;
}
-(void)update:(ccTime)dt {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults synchronize];
CGPoint jumpForce = ccp(0.0, 310.0);
float jumpCutoff = 150.0;
if (self.mightAsWellJump && self.onGround) {
self.velocity = ccpAdd(self.velocity, jumpForce);
if (![defaults boolForKey:@"All Muted"]) {
if (![defaults boolForKey:@"SFX Muted"]) {
[[SimpleAudioEngine sharedEngine] playEffect:@"jump.wav"];
}
}
} else if (!self.mightAsWellJump && self.velocity.y > jumpCutoff) {
self.velocity = ccp(self.velocity.x, jumpCutoff);
}
CGPoint gravity = ccp(0.0, -450.0);
CGPoint gravityStep = ccpMult(gravity, dt);
CGPoint forwardMove = ccp(800.0, 0.0);
CGPoint forwardStep = ccpMult(forwardMove, dt);
self.velocity = ccp(self.velocity.x * 0.90, self.velocity.y); //2
if (self.forwardMarch) {
self.velocity = ccpAdd(self.velocity, forwardStep);
if (!self.isMoving) {
//[self runAction: _WalkAction];
//self.isMoving = YES;
}
} //3
CGPoint minMovement = ccp(0.0, -450.0);
CGPoint maxMovement = ccp(120.0, 250.0);
self.velocity = ccpClamp(self.velocity, minMovement, maxMovement);
self.velocity = ccpAdd(self.velocity, gravityStep);
CGPoint stepVelocity = ccpMult(self.velocity, dt);
if (!self.isGoingLeft) {
self.desiredPosition = ccpAdd(self.position, stepVelocity);
}else{
self.desiredPosition = ccp(self.position.x-stepVelocity.x, self.position.y+stepVelocity.y);
}
}
-(CGRect)collisionBoundingBox {
CGRect collisionBox = CGRectInset(self.boundingBox, 3, 0);
//CGRect collisionBox = self.boundingBox;
//collisionBox = CGRectOffset(collisionBox, 0, -2);
CGPoint diff = ccpSub(self.desiredPosition, self.position);
CGRect returnBoundingBox = CGRectOffset(collisionBox, diff.x, diff.y);
return returnBoundingBox;
}
@end
所以现在的问题是,我如何让玩家像普通精灵一样出现在游戏中,然后当玩家开始向前移动时我如何运行动画