我正在寻找一些关于如何使用Cocos2d,我可以拥有一个以恒定速度持续运动的物体(让他们说火箭)的指导,我只有两个按钮来简单改变方向。
我已经找到了一些关于如何改变火箭方向的信息,但是我只是坚持让火箭不断运动。
我后来想让火箭能够从屏幕上飞出并重新出现在屏幕的另一侧,但那是另一个问题(我发现了一些问题)已经帮助了。)
我是Cocos2d的新手并且已经搜索了几个小时,但我们还没有找到我之后的内容。
能否让我的火箭不断移动,并对方向的变化作出反应,有人能指出我正确的方向吗?
答案 0 :(得分:0)
所以,这是第一步,你可以做些什么:
// ProjectileTest拥有一个只知道如何操作的射弹类 移动自己
// WrapBoundaryTest是显示潜在用法的示例场景 ProjectileTest
<强> ProjectileTest.h 强>
#import "cocos2d.h"
@interface ProjectileTest : CCNode
{
CCSprite *sprite;
CGPoint velocity;
CGRect bounds;
BOOL isActive;
float steerAmount;
}
@property CCSprite *sprite;
@property CGPoint velocity;
@property CGRect bounds;
@property BOOL isActive;
@property float steerAmount;
+(id) projectileWithBounds:(CGRect) boundary
andVelocity:(CGPoint) v
andSteeringAmount:(float) steeringAmount;
-(void) step:(CCTime) dt;
-(void) steerLeft;
-(void) steerRight;
@end
<强> ProjectileTest.m 强>
#import "ProjectileTest.h"
@implementation ProjectileTest
@synthesize sprite, velocity, bounds, isActive, steerAmount;
+(id) projectileWithBounds:(CGRect) boundary andVelocity:(CGPoint) v andSteeringAmount:(float) steeringAmount
{
ProjectileTest *proj = [[self alloc] init];
proj.bounds = boundary;
proj.velocity = v;
proj.steerAmount = steeringAmount;
proj.isActive = YES;
[proj calculateAngleForVelocity: v];
return proj;
}
-(id) init
{
if(( self = [super init]))
{
sprite = [CCSprite spriteWithImageNamed:@"Icon.png"];
[self addChild:sprite];
[sprite setScale:0.50f];
bounds = CGRectZero;
velocity = CGPointZero;
isActive = NO;
steerAmount = 1.0f;
}
return self;
}
-(void) calculateAngleForVelocity:(CGPoint) v
{
float rads = ccpToAngle(v);
float degs = -CC_RADIANS_TO_DEGREES(rads);
sprite.rotation = degs + 90.0f;
}
-(void) steerLeft
{
[self steer: YES];
}
-(void) steerRight
{
[self steer: NO];
}
-(void) steer:(BOOL) left
{
if(left)
{
velocity = ccpRotateByAngle(velocity, CGPointZero, -CC_DEGREES_TO_RADIANS(-steerAmount));
}
else // right
{
velocity = ccpRotateByAngle(velocity, CGPointZero, -CC_DEGREES_TO_RADIANS(steerAmount));
}
}
-(void) step:(CCTime)dt
{
if(isActive)
{
if(CGRectContainsPoint(bounds, self.position))
{
self.position = ccpAdd(self.position, velocity);
[self calculateAngleForVelocity: velocity];
}
else
{
float nudge = 0.5f;
if(self.position.x >= bounds.size.width && velocity.x > 0.0f)
{
self.position = ccp(bounds.origin.x + nudge, self.position.y);
}
else if(self.position.x <= bounds.origin.x && velocity.x < 0.0f)
{
self.position = ccp(bounds.size.width - nudge, self.position.y);
}
if(self.position.y >= bounds.size.height && velocity.y > 0.0f)
{
self.position = ccp(self.position.x, bounds.origin.y + nudge);
}
else if(self.position.y <= bounds.origin.y && velocity.y < 0.0f)
{
self.position = ccp(self.position.x, bounds.size.height - nudge);
}
}
}
}
@end
<强> WrapBoundaryTest.h 强>
#import "cocos2d.h"
#import "cocos2d-ui.h"
@interface WrapBoundaryTest : CCScene
{
NSMutableArray *projectiles;
CCButton *left;
CCButton *right;
}
@property __strong NSMutableArray *projectiles;
+(id) scene;
@end
<强> WrapBoundaryTest.m 强>
#import "WrapBoundaryTest.h"
#import "ProjectileTest.h"
@implementation WrapBoundaryTest
@synthesize projectiles;
+(id) scene
{
return [[self alloc] init];
}
-(id) init
{
if(( self = [super init]))
{
projectiles = [NSMutableArray array];
CGRect projectileBounds = CGRectMake(
0.0f,
0.0f,
[CCDirector sharedDirector].designSize.width,
[CCDirector sharedDirector].designSize.height
);
CGPoint origin = ccp(200.0f, 150.0f);
[self addProjectileWithBoundsRect:projectileBounds andVelocity:ccp( 1.0f, 5.0f) andSteeringAmount:2.0f atStartingPosition:origin];
[self addProjectileWithBoundsRect:projectileBounds andVelocity:ccp(-1.0f, -5.0f) andSteeringAmount:1.0f atStartingPosition:origin];
[self addProjectileWithBoundsRect:projectileBounds andVelocity:ccp(-1.0f, 1.0f) andSteeringAmount:0.5f atStartingPosition:origin];
[self addProjectileWithBoundsRect:projectileBounds andVelocity:ccp( 8.0f, 1.0f) andSteeringAmount:7.5f atStartingPosition:origin];
left = [CCButton buttonWithTitle:@"Left" fontName:@"Arial" fontSize:16.0f];
left.positionType = CCPositionTypeNormalized;
left.position = ccp(0.1f, 0.1f);
[self addChild:left];
right = [CCButton buttonWithTitle:@"Right" fontName:@"Arial" fontSize:16.0f];
right.positionType = CCPositionTypeNormalized;
right.position = ccp(0.9f, 0.1f);
[self addChild:right];
}
return self;
}
-(void) addProjectileWithBoundsRect:(CGRect) boundsRect
andVelocity:(CGPoint) velocity
andSteeringAmount:(float) steeringAmount
atStartingPosition:(CGPoint) startingPosition
{
ProjectileTest *proj = [ProjectileTest projectileWithBounds: boundsRect
andVelocity: velocity
andSteeringAmount: steeringAmount
];
proj.position = startingPosition;
[self addChild:proj];
[projectiles addObject:proj];
}
-(void) update:(CCTime)delta
{
for(ProjectileTest *p in projectiles)
{
if(left.touchInside)
{
[p steerLeft];
}
if(right.touchInside)
{
[p steerRight];
}
[p step:delta];
}
}
@end
// HTH