Sprite kit Can';无法在正确的位置生成项目符号图像
嗨,伙计们,我有一个精灵套件加农炮游戏,它工作得很好,但有一个问题。如果我将加农炮指向图像中的顶部或底部,子弹因为是从加农炮精灵的中心产生的,它清楚地表明子弹不是从加农炮的通道发射的,而是从图像的中心发射的,我怎样才能确定子弹的位置,使它总是从隧道口产生 这是到目前为止我的代码Sprite kit Can';无法在正确的位置生成项目符号图像,sprite-kit,Sprite Kit,嗨,伙计们,我有一个精灵套件加农炮游戏,它工作得很好,但有一个问题。如果我将加农炮指向图像中的顶部或底部,子弹因为是从加农炮精灵的中心产生的,它清楚地表明子弹不是从加农炮的通道发射的,而是从图像的中心发射的,我怎样才能确定子弹的位置,使它总是从隧道口产生 这是到目前为止我的代码 - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { CGPoint location = [_Player position
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint location = [_Player position];
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self.scene];
bullet = [SKSpriteNode spriteNodeWithImageNamed:@"cannonbullet"];
bullet.xScale = 0.06;
bullet.yScale = 0.06;
bullet.position =
CGPointMake(location.x+_Player.zRotation,location.y+_Player.zRotation);
bullet.zPosition = 0;
CGPoint offset = rwSub(touchLocation, bullet.position);
if (offset.x <= 0) return;
[self addChild:bullet];
CGPoint direction = rwNormalize(offset);
CGPoint shootAmount = rwMult(direction, 400);
CGPoint realDest = rwAdd(shootAmount, bullet.position);
float velocity = 480.0/1.0;
float realMoveDuration = self.size.width / velocity;
SKAction * actionMove = [SKAction moveTo:realDest duration:realMoveDuration];
SKAction * actionMoveDone = [SKAction removeFromParent];
[bullet runAction:[SKAction sequence:@[actionMove, actionMoveDone]]];
[self animStarter];
}
-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event{
CGPoint位置=[[玩家位置];
UITouch*touch=[触摸任何对象];
CGPoint-touchLocation=[touch-locationInNode:self.scene];
bullet=[SKSpriteNode SPRITENODEWITHIMAGENAME:@“cannonbullet”];
bullet.xScale=0.06;
bullet.yScale=0.06;
bullet.position=
CGPointMake(location.x+\u Player.zRotation,location.y+\u Player.zRotation);
bullet.zPosition=0;
CGPoint offset=rwSub(接触位置,项目符号位置);
如果(offset.x确保你的炮管精灵图像与图像的中心x轴对齐(参见图片)。如果你想复制项目,你可以拖出或复制图像
下面是将加农炮旋转到触碰位置并在达到所需旋转角度后发射炮弹的代码。我认为您仍在使用僵尸康加代码,因此为了方便起见,我使用了它的功能
#import "MyScene.h"
static inline CGPoint CGPointSubtract(const CGPoint a,
const CGPoint b)
{
return CGPointMake(a.x - b.x, a.y - b.y);
}
static inline CGPoint CGPointMultiplyScalar(const CGPoint a,const CGFloat b)
{
return CGPointMake(a.x * b, a.y * b);
}
static inline CGFloat CGPointLength(const CGPoint a)
{
return sqrtf(a.x * a.x + a.y * a.y);
}
static inline CGPoint CGPointNormalize(const CGPoint a)
{
CGFloat length = CGPointLength(a);
return CGPointMake(a.x / length, a.y / length);
}
static inline CGFloat CGPointToAngle(const CGPoint a)
{
return atan2f(a.y, a.x);
}
static inline CGFloat ScalarSign(CGFloat a)
{
return a >= 0 ? 1 : -1;
}
static inline CGFloat ScalarShortestAngleBetween(const CGFloat a, const CGFloat b)
{
CGFloat difference = b - a;
CGFloat angle = fmodf(difference, M_PI * 2);
if (angle >= M_PI) {
angle -= M_PI * 2;
}
return angle;
}
static const float ROTATE_RADIANS_PER_SEC = 4 * M_PI;
static const float MOVE_POINTS_PER_SEC = 120.0;
@implementation MyScene
{
SKSpriteNode *cannon;
NSTimeInterval _lastUpdateTime;
NSTimeInterval _dt;
CGPoint _velocity;
CGPoint _lastTouchLocation;
BOOL fireCannon;
CGPoint destination;
}
-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size])
{
[self createCannon];
fireCannon = false;
}
return self;
}
-(void)createCannon
{
cannon = [SKSpriteNode spriteNodeWithImageNamed:@"cannon"];
cannon.position = CGPointMake(self.size.height/2, self.size.width/2);
[self addChild:cannon];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self.scene];
fireCannon = true;
destination = touchLocation;
_lastTouchLocation = touchLocation;
CGPoint offset = CGPointSubtract(touchLocation, cannon.position);
CGPoint direction = CGPointNormalize(offset);
_velocity = CGPointMultiplyScalar(direction, MOVE_POINTS_PER_SEC);
}
-(void)update:(CFTimeInterval)currentTime
{
if (_lastUpdateTime) {
_dt = currentTime - _lastUpdateTime;
} else {
_dt = 0;
}
_lastUpdateTime = currentTime;
[self rotateSprite:cannon toFace:_velocity rotateRadiansPerSec:ROTATE_RADIANS_PER_SEC];
}
- (void)rotateSprite:(SKSpriteNode *)sprite
toFace:(CGPoint)velocity
rotateRadiansPerSec:(CGFloat)rotateRadiansPerSec
{
float targetAngle = CGPointToAngle(velocity);
float shortest = ScalarShortestAngleBetween(cannon.zRotation, targetAngle);
float amtToRotate = rotateRadiansPerSec * _dt;
if (ABS(shortest) < amtToRotate)
{
amtToRotate = ABS(shortest);
}
sprite.zRotation += ScalarSign(shortest) * amtToRotate;
if ((ABS(shortest) == amtToRotate) && (fireCannon == true))
{
fireCannon = false;
[self fire:targetAngle];
}
}
-(void)fire:(float)targetAngle
{
SKSpriteNode *cannonBall = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(5, 5)];
cannonBall.position = cannon.position;
[self addChild:cannonBall];
int x = cannon.position.x + 1000 * cos(targetAngle);
int y = cannon.position.y + 1000 * sin(targetAngle);
[cannonBall runAction:[SKAction moveTo:CGPointMake(x, y) duration:2]];
}
@end
#导入“MyScene.h”
静态内联CGPoint CGPointSubtract(常量CGPoint a,
常数(点b)
{
返回CGPointMake(a.x-b.x,a.y-b.y);
}
静态内联CGPoint cgpointmultilyscalar(常量CGPoint a,常量cgploat b)
{
返回CGPointMake(a.x*b,a.y*b);
}
静态内联CGFloat CGPointLength(常量CGPoint a)
{
返回sqrtf(a.x*a.x+a.y*a.y);
}
静态内联CGPoint CGPointNormalize(常量CGPoint a)
{
CGFloat长度=CGPointLength(a);
返回CGPointMake(a.x/长度,a.y/长度);
}
静态内联CGFloat CGPointToAngle(常量CGPoint a)
{
返回atan2f(a.y,a.x);
}
静态内联CGFloat标量符号(CGFloat a)
{
返回a>=0?1:-1;
}
静态内联CGFloat scalarShortTestAngleBetween(常量CGFloat a,常量CGFloat b)
{
CGFloat差=b-a;
CGFloat角度=fmodf(差值,M_PI*2);
如果(角度>=M_PI){
角度-=M_PI*2;
}
返回角;
}
静态常数浮点旋转弧度/秒=4*M\uπ;
静态常数浮点移动点/秒=120.0;
@MyScene的实现
{
斯普林泰诺德*加农炮;
NSTimeInterval\u lastUpdateTime;
n时间间隔_dt;
CGU点速度;
CGPoint\u lastTouchLocation;
布尔火炮;
CGPoint目的地;
}
-(id)initWithSize:(CGSize)大小
{
if(self=[super initWithSize:size])
{
[自创加农炮];
火炮=假;
}
回归自我;
}
-(无效)创建加农炮
{
cannon=[SKSpriteNode SPRITENODEWITHIMAGENAME:@“cannon”];
cannon.position=CGPointMake(self.size.height/2,self.size.width/2);
[自我添加:加农炮];
}
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件
{
UITouch*touch=[触摸任何对象];
CGPoint-touchLocation=[touch-locationInNode:self.scene];
火炮=真;
目的地=触摸位置;
_lastTouchLocation=触摸位置;
CGPoint offset=CGPointSubtract(触控位置,加农炮位置);
CGPoint方向=CGPointNormalize(偏移);
_速度=cgpointmultilyscalar(方向、移动点/秒);
}
-(无效)更新:(CFTimeInterval)currentTime
{
如果(_lastUpdateTime){
_dt=当前时间-\u上次更新时间;
}否则{
_dt=0;
}
_lastUpdateTime=currentTime;
[自旋转脚本:加农炮到面:_速度旋转半径:旋转弧度每_秒];
}
-(空)旋转斜纹:(SKSpriteNode*)斜纹
toFace:(CGPoint)速度
rotateRadiansPerSec:(CGFloat)rotateRadiansPerSec
{
浮动目标角度=cg点到角度(速度);
float shortest=标度shortestanglebetween(cannon.zRotation,targetAngle);
float amtToRotate=rotateRadiansPerSec*\u dt;
if(ABS(最短)
如果我能见到你,我会买一顿午餐。“你太棒了。”“这很管用。我不知道该如何感谢你。但我非常感谢你。