Ios cocos2d定位
问题是,我的射弹的起始和结束位置有点偏离。我有Ios cocos2d定位,ios,cocos2d-iphone,Ios,Cocos2d Iphone,问题是,我的射弹的起始和结束位置有点偏离。我有NSLog'd所有CGPoints,从中可以看出,输入CCSequence的点是正确的。但从我在屏幕上看到的情况来看,情况有所不同 我认为有一个很大的线索是,如果创建一个新的CCSprite,并像使用投射物一样使用它,那么路径就会工作 下面是游戏级别层(游戏级别:Level1Layer)上的AddSprojects方法 这些NSLog语句的输出是(仅供参考:射弹是从self.wizardHero添加的,因此这是它的起始位置;目标是self.redEn
NSLog
'd所有CGPoints
,从中可以看出,输入CCSequence
的点是正确的。但从我在屏幕上看到的情况来看,情况有所不同
我认为有一个很大的线索是,如果创建一个新的CCSprite,并像使用投射物一样使用它,那么路径就会工作
下面是游戏级别层(游戏级别:Level1Layer)上的AddSprojects方法
这些NSLog
语句的输出是(仅供参考:射弹是从self.wizardHero
添加的,因此这是它的起始位置;目标是self.redEnemy
):
2013-03-27 10:41:57.295网格战[13025:c07]射弹位置{105,
178}
2013-03-27 10:41:57.296网格战争[13025:c07]向导位置{105178}
2013-03-27 10:41:57.297网格战争[13025:c07]向导定位世界空间
{105178}
2013-03-27 10:41:57.298网格战争[13025:c07]目的地位置{299,
174}
2013-03-27 11:34:46.608网格战争[13344:c07]realDest{650,
166}
2013-03-27 11:34:46.608网格战争[13344:c07]长度545.132080
2013-03-27 11:34:46.610网格战争[13344:c07]实时移动持续时间1.946900
我意识到,对于超级类,我有点难以判断到底发生了什么(有点像为什么我要包含源代码)。基本上,虽然我有三个主要的超类,它们是从CCNode
Projectiles : CCNode
sprite : CCSprite
destination : GameCharacters
damage : int
GameLevels : CCNode
hud : HudLayer
tileMap : CCTMXTiledMap
enemies : NSMutablArray
projectiles : NSMutablArray
GameCharacters : CCNode
hp : int
juice : int
sprite : CCSprite
selectedTargets : NSMutablArray
hostLayer : GameLevels
然后是实际实现这些的三个类
BasicProjectile : Projectiles
Level1Layer : GameLevels
WizardHero : GameCharacter
RedEnemy : GameCharacter
虽然这个问题有了一些进展,但我已经将其发布在了上,并收到了一些帮助,这使我达到了这一点(看到了投射物,但路径是错误的,而不是根本看不到它们)。但是几天过去了,希望能得到更多的帮助
游戏投射物:CCNode
#import "CCNode.h"
//forward declaration because we just need to hold a reference
@class GameCharacters;
@interface GameProjectiles : CCNode
@property(nonatomic, strong) CCSprite * sprite;
@property(nonatomic,strong) GameCharacters * destination;
@property(nonatomic) int damage;
-(id)initWithDestination:(GameCharacters*)Destination;
-(CGSize)contentSize;
@end
#import "GameProjectiles.h"
@implementation GameProjectiles
-(id)initWithDestination:(GameCharacters*)Destination
{
if (self = [super init]) {
_damage = 0;
_destination = Destination;
}
return self;
}
-(CGSize)contentSize{
return self.sprite.contentSize;
}
-(void)setPosition:(CGPoint)position
{
[super setPosition:position];
self.sprite.position = position;
}
@end
#import "GameProjectiles.h"
@interface BasicProjectile : GameProjectiles
@end
#import "BasicProjectile.h"
@implementation BasicProjectile
-(id)initWithDestination:(GameCharacters*)Destination
{
if (self = [super init]) {
self.damage = 25;
self.sprite = [CCSprite spriteWithFile:@"Projectile.png"];
[self addChild:self.sprite z:1000 ];
self.destination = Destination;
}
return self;
}
@end
基本项目
#import "CCNode.h"
//forward declaration because we just need to hold a reference
@class GameCharacters;
@interface GameProjectiles : CCNode
@property(nonatomic, strong) CCSprite * sprite;
@property(nonatomic,strong) GameCharacters * destination;
@property(nonatomic) int damage;
-(id)initWithDestination:(GameCharacters*)Destination;
-(CGSize)contentSize;
@end
#import "GameProjectiles.h"
@implementation GameProjectiles
-(id)initWithDestination:(GameCharacters*)Destination
{
if (self = [super init]) {
_damage = 0;
_destination = Destination;
}
return self;
}
-(CGSize)contentSize{
return self.sprite.contentSize;
}
-(void)setPosition:(CGPoint)position
{
[super setPosition:position];
self.sprite.position = position;
}
@end
#import "GameProjectiles.h"
@interface BasicProjectile : GameProjectiles
@end
#import "BasicProjectile.h"
@implementation BasicProjectile
-(id)initWithDestination:(GameCharacters*)Destination
{
if (self = [super init]) {
self.damage = 25;
self.sprite = [CCSprite spriteWithFile:@"Projectile.png"];
[self addChild:self.sprite z:1000 ];
self.destination = Destination;
}
return self;
}
@end
由于投射物和目标位于单独的CCNode内,因此它们的边界框将返回它们在其父节点内的位置,而不是在实际屏幕坐标中(在本例中需要) 要转换pos,您可以这样做
CGPoint targetPos = [basicProjectile.destination.sprite.parent convertToWorldSpace:basicProjectile.destination.sprite.position];
NSLog(@"targetPos %@",NSStringFromCGPoint(targetPos));
CGRect targetRect = [self boundingRectForSprite:basicProjectile.destination.sprite andPos:targetPos];
CGPoint projectilePos = [basicProjectile.sprite.parent convertToWorldSpace:basicProjectile.sprite.position];
NSLog(@"projectilePos %@",NSStringFromCGPoint(projectilePos));
CGRect projectileRect = [self boundingRectForSprite:basicProjectile.destination.sprite andPos:projectilePos];
boundingRectForSprite
方法使使用sprite边界框和位置,然后计算最终的rect以锚定点(0.5,0.5)
现在,
targetlect
和projectlerect
是实际屏幕坐标。您在屏幕上看到了什么?log readDest、length和realMoveDuration plz。射弹位于遥远的北方(顶部),它可能太偏右,但太偏北很难看到。我会用这些值更新上面的代码,一秒钟,如果你能看到向导,而不是投射物,它们都有相同的位置!!!我只能假设他们的“位置”是相对于不同的东西(有不同的父母)。。。这也可能与目标对象不同:)@YvesLeBorg我看到了投射物。经过更多的测试,如果我拿出我的定制投射物,在里面放一个普通的CCSprite,一切正常。我看到了问题:在游戏投射物中,你覆盖了设置位置,你将嵌入的sprite的位置设置为相同的值。精灵的位置应为(0,0)。按照您的方式,它最终会(2x,2y)