使用类和定位

时间:2015-08-07 09:00:44

标签: ios sprite-kit

如果我将myShip加载到游戏图层上,操纵杆控件可以正常工作

E.G

  myShip = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship.png"];
  [myShip setScale:0.5];
  myShip.position = CGPointMake(CGRectGetMidX(self.frame),    CGRectGetMidY(self.frame));
[self addChild:myShip];

(我这样做是为了测试控制器代码,稍后想到制作一个shipClass,然后将其添加到图层并使用控制器来控制shipClass位置 但是当使用这个代码时,ShipClass位于0,0并且不会随控件一起移动,我做错了什么?

myShipshipClass = [[ShipClass alloc]initWith:self];

这是我第一次开始上课,所以我知道它缺少的东西......

从概念上讲,我一定是做错了什么...... 我很想知道为什么这个位置会被改变,不再适用于控制器,以及如何修复它以便它可以工作。

-(void)update:(CFTimeInterval)currentTime {

[myMeteor updateMeteor:.02];


// Shoot bullets every half second
if ( currentTime - lastUpdate >= 0.5 ){
    [self shootBullet];
    lastUpdate = currentTime;
}

if ( moveJoystick.isMoving )
{
    NSLog(@" if update:moveJoystick.isMoving");
    CGPoint adjustedSpritePosition
    = CGPointMake(self.myShip02.position.x + moveJoystick.moveSize.width,
                  self.myShip02.position.y + moveJoystick.moveSize.height);

    if ( adjustedSpritePosition.x < 0 ){
        adjustedSpritePosition.x = 0;
    } else if ( adjustedSpritePosition.x > self.size.width ){
        adjustedSpritePosition.x = self.size.width;
    }
    if ( adjustedSpritePosition.y < 0 ){
        adjustedSpritePosition.y = 0;
    } else if ( adjustedSpritePosition.y > self.size.height ){
        adjustedSpritePosition.y = self.size.height;
    }

    myShip02.position = adjustedSpritePosition;
}

}

// ShipCLass

这是我改变和学习使课程开始工作的原因

- (instancetype)initWithImageNamed:(NSString *)name
{
    if (self = [super initWithImageNamed:name]) {
      //numOfDamages = 0;
      //numOfBullets = 100;
    }
    return self;
}

// ShipCLass.h

#import <SpriteKit/SpriteKit.h>
#import "GameScene.h"
#import <CoreMotion/CoreMotion.h>

@class SpriteBar;
@interface ShipClass : SKSpriteNode <UIAccelerometerDelegate,SKPhysicsContactDelegate>
{
   GameScene *layer;

    //health
    int numOfDamages;

    //bullets
    int numOfBullets;
}

@property (nonatomic, retain) SKNode *shipShield;
@property (nonatomic, retain) SKPhysicsBody *body;
@property (nonatomic, assign) int numOfDamages;
@property (nonatomic, assign) int numOfBullets;

-(id)initWith:(GameScene *)gameLayer;
@property(strong) SKTextureAtlas *atlas;

@end

// ShootBullet

    -(void)shootBullet{
   SKSpriteNode *bullet = [SKSpriteNode spriteNodeWithImageNamed:@"bullet"];
    bullet.position = myShip02.position;
    [self addChild:bullet];

CGPoint movePoint = [self destPointForAngle:shootJoystick.angle];
CGPoint adjustedPoint = CGPointMake(myShip02.position.x + movePoint.x, myShip02.position.y + movePoint.y);

SKAction *moveAction = [SKAction moveTo:adjustedPoint duration:1];
SKAction *removeAction = [SKAction removeFromParent];
[bullet runAction:[SKAction sequence:@[moveAction, removeAction]]];

}

// 2个问题,子弹节点添加到游戏层上的船上 现在只有子弹节点可见(船舶坐标必须是错误的,或者节点与shipClass的关系是错误的?)

子弹头位置不等于船位

1 个答案:

答案 0 :(得分:0)

我找到了解决方案之后,一些成员非常友好地提供了一些如何使课程发挥作用的良好提示。 - 解决方案代码

**.m**
- (instancetype)initWithImageNamed:(NSString *)name
{
    if (self = [super initWithImageNamed:name]) {
      //cool stuff here
    }
    return self;
}
// 2.0 and above
@Override
public void onBackPressed() {
    moveTaskToBack(true);
}

// Before 2.0
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

这样做修复了坐标问题并简化了制作课程的整个过程。用更少的代码编写,谢谢大家!