Ios 加速度计介绍?代码修复?

Ios 加速度计介绍?代码修复?,ios,iphone,xcode,accelerometer,move,Ios,Iphone,Xcode,Accelerometer,Move,最新错误: 我最近正在开发一个有教程的应用程序,现在我想放弃教程。但是我几乎没有使用SpriteKit的经验,所以我想得到一些帮助,帮助我使用加速计来控制应用程序上的某些东西 游戏中你控制太空船的部分如下: override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { for touch: AnyObject in touches{ let p

最新错误:

我最近正在开发一个有教程的应用程序,现在我想放弃教程。但是我几乎没有使用SpriteKit的经验,所以我想得到一些帮助,帮助我使用加速计来控制应用程序上的某些东西

游戏中你控制太空船的部分如下:

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

        for touch: AnyObject in touches{

            let pointOfTouch = touch.locationInNode(self)
            let previousPointOfTouch = touch.previousLocationInNode(self)

            let amountDragged = pointOfTouch.x - previousPointOfTouch.x

            player.position.x  += amountDragged

            if player.position.x > CGRectGetMaxX(gameArea) - player.size.width / 2{

                player.position.x = CGRectGetMaxX(gameArea) - player.size.width / 2


            }

            if player.position.x < CGRectGetMinX(gameArea) + player.size.width / 2{
                player.position.x = CGRectGetMinX(gameArea) + player.size.width / 2    
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
用于触摸:触摸中的任何对象{
let pointOfTouch=触摸位置节点(自身)
让previousPointOfTouch=触摸。previousLocationInNode(自身)
设AmountDrawed=pointOfTouch.x-上一个pointOfTouch.x
player.position.x+=数量
如果player.position.x>CGRectGetMaxX(游戏区域)-player.size.width/2{
player.position.x=CGRectGetMaxX(游戏区域)-player.size.width/2
}
如果player.position.x
所以,任何人都可以编辑我的代码,让我使用加速计来控制飞船,或者我们可以通过Skype聊天,我需要一些帮助。我的Skype是RoobTheMan

//  GameScene.swift
//  One Mission
//
//  Created by Robert Smith on 7/8/16.
//  Copyright (c) 2016 RobTheMan. All rights reserved.
//

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate{


    let bulletSound = SKAction.playSoundFileNamed("BulletSound.wav" , waitForCompletion: false)


    struct physicsCategories {
        static let None : UInt32 = 0
        static let Player : UInt32 = 0b1 // 1
        static let Bullet : UInt32 = 0b10 //2
        static let Enemy : UInt32 = 0b100 // 4

    }

    let player = SKSpriteNode(imageNamed: "playerShip")


    func random() -> CGFloat {
        return CGFloat(Float(arc4random()) / 0xFFFFFFFF)

    }
    func random(min min: CGFloat, max: CGFloat) -> CGFloat{
        return random() * (max - min) + min
    }      


    let gameArea: CGRect

    override init(size: CGSize) {

        let maxAspectRatio: CGFloat = 16.0/9.0
        let playableWidth = size.height / maxAspectRatio
        let margin = (size.width - playableWidth) / 2
        gameArea = CGRect(x: margin, y: 0 , width: playableWidth, height: size.height)


        super.init(size: size)

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    override func didMoveToView(view: SKView) {

        self.physicsWorld.contactDelegate = self

        let background = SKSpriteNode(imageNamed: "background")
        background.size = self.size
        background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
        background.zPosition = 0
        self.addChild(background)


        player.setScale(1)
        player.position = CGPoint(x: self.size.width/2, y: self.size.height * 0.2)
        player.zPosition = 2
        player.physicsBody = SKPhysicsBody(rectangleOfSize:  player.size)
        player.physicsBody!.affectedByGravity = false
        player.physicsBody!.categoryBitMask = physicsCategories.Player
        player.physicsBody!.collisionBitMask = physicsCategories.None
        player.physicsBody!.contactTestBitMask = physicsCategories.Enemy
        self.addChild(player)

        startNewLevel()

    }


    func startNewLevel(){


        let spawn = SKAction.runBlock(spawnEnemy)
        let waitToSpawn = SKAction.waitForDuration(1)
        let spawnSequence = SKAction.sequence([spawn, waitToSpawn])
        let spawnForever = SKAction.repeatActionForever(spawnSequence)
        self.runAction(spawnForever)


    }


    func fireBullet() {

        let bullet = SKSpriteNode(imageNamed: "bullet")
        bullet.setScale(0.8)
        bullet.position = player.position
        bullet.zPosition = 1
        bullet.physicsBody = SKPhysicsBody(rectangleOfSize: bullet.size)
        bullet.physicsBody!.affectedByGravity = false
        bullet.physicsBody!.categoryBitMask = physicsCategories.Bullet
        bullet.physicsBody!.collisionBitMask = physicsCategories.None
        bullet.physicsBody!.contactTestBitMask = physicsCategories.Enemy
        self.addChild(bullet)

        let moveBullet = SKAction.moveToY(self.size.height + bullet.size.height, duration: 1)
        let deleteBullet = SKAction.removeFromParent()
        let bulletSequence = SKAction.sequence([bulletSound, moveBullet, deleteBullet])
        bullet.runAction(bulletSequence)


    }


    func spawnEnemy(){

        let randomXStart = random(min: CGRectGetMinX(gameArea), max: CGRectGetMaxX(gameArea))
        let randomXEnd = random(min: CGRectGetMinX(gameArea), max: CGRectGetMaxX(gameArea))

        let startPoint = CGPoint(x: randomXStart, y: self.size.height * 1.2)
        let endPoint = CGPoint(x: randomXEnd, y: -self.size.height * 0.2)

        let enemy = SKSpriteNode(imageNamed: "enemyShip")
        enemy.setScale(1)
        enemy.position = startPoint
        enemy.zPosition = 2
        enemy.physicsBody = SKPhysicsBody(rectangleOfSize: enemy.size)
        enemy.physicsBody!.affectedByGravity = false
        enemy.physicsBody!.categoryBitMask = physicsCategories.Enemy
        enemy.physicsBody!.categoryBitMask = physicsCategories.None
        enemy.physicsBody!.contactTestBitMask = physicsCategories.Player | physicsCategories.Bullet
        self.addChild(enemy)

        let moveEnemy = SKAction.moveTo(endPoint, duration: 1.5)

        let deleteEnemy = SKAction.removeFromParent()
        let enemySequence = SKAction.sequence([moveEnemy, deleteEnemy])
        enemy.runAction(enemySequence)

        let dx = endPoint.x - startPoint.x
        let dy = endPoint.y - startPoint.y
        let amountToRotate = atan2(dy, dx)
        enemy.zRotation = amountToRotate

    }


    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        fireBullet()
    }


    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

        for touch: AnyObject in touches{

            let pointOfTouch = touch.locationInNode(self)
            let previousPointOfTouch = touch.previousLocationInNode(self)

            let amountDragged = pointOfTouch.x - previousPointOfTouch.x

            player.position.x  += amountDragged

            if player.position.x > CGRectGetMaxX(gameArea) - player.size.width / 2{

                player.position.x = CGRectGetMaxX(gameArea) - player.size.width / 2


            }

            if player.position.x < CGRectGetMinX(gameArea) + player.size.width / 2{
                player.position.x = CGRectGetMinX(gameArea) + player.size.width / 2
            }

        }

    }

}
//GameScene.swift
//一次任务
//
//罗伯特·史密斯于2016年7月8日创作。
//版权所有(c)2016 RobTheMan。保留所有权利。
//
进口SpriteKit
类游戏场景:SKScene,SKPhysicContactDelegate{
让bulletSound=SKAction.PlaySoundFileName(“bulletSound.wav”,waitForCompletion:false)
结构物理分类{
静态let None:UInt32=0
静态let播放器:UInt32=0b1//1
静态let项目符号:UInt32=0b10//2
静态让敌人:UInt32=0b100//4
}
let player=SKSpriteNode(图像名为:“玩家”)
func random()->CGFloat{
返回CGFloat(Float(arc4random())/0xFFFFFFFF)
}
func随机(最小值:CGFloat,最大值:CGFloat)->CGFloat{
返回random()*(最大-最小)+min
}      
让游戏区:CGRect
重写初始化(大小:CGSize){
设maxAspectRatio:CGFloat=16.0/9.0
让playableWidth=size.height/maxAspectRatio
让边距=(size.width-playableWidth)/2
gameArea=CGRect(x:margin,y:0,width:playableWidth,height:size.height)
super.init(大小:size)
}
必需的初始化?(编码器aDecoder:NSCoder){
fatalError(“初始化(编码者:)尚未实现”)
}
覆盖func didMoveToView(视图:SKView){
self.physicsWorld.contactDelegate=self
让background=SKSpriteNode(图像名为:“background”)
background.size=self.size
background.position=CGPoint(x:self.size.width/2,y:self.size.height/2)
background.zPosition=0
self.addChild(背景)
播放器设置刻度(1)
player.position=CGPoint(x:self.size.width/2,y:self.size.height*0.2)
player.zPosition=2
player.physicsBody=SKPhysicsBody(矩形大小:player.size)
player.physicsBody!.affectedByGravity=false
player.physicsBody!.categoryBitMask=physiccategories.player
player.physicsBody!.collisionBitMask=physicCategories.None
player.physicsBody!.contactTestBitMask=physicCategories.敌军
self.addChild(播放器)
开始新级别()
}
func startNewLevel(){
让spawn=SKAction.runBlock(spawn敌人)
让waitToSpawn=SKAction.waitForDuration(1)
让spawnSequence=SKAction.sequence([spawn,waitToSpawn])
让spawnForever=SKAction.repeatActionForever(spawnSequence)
self.runAction(永远生成)
}
func fireball(){
让bullet=SKSpriteNode(图像名为:“bullet”)
项目符号。设置刻度(0.8)
bullet.position=player.position
bullet.zPosition=1
bullet.physicsBody=SKPhysicsBody(矩形尺寸:bullet.size)
bullet.physicsBody!。受重力影响=false
bullet.physicsBody!.categoryBitMask=physicCategories.bullet
bullet.physicsBody!.collisionBitMask=physicsCategories.None
bullet.physicsBody!.contactTestBitMask=physicCategories.敌军
self.addChild(项目符号)
让moveBullet=SKAction.moveToY(self.size.height+bullet.size.height,持续时间:1)
让deleteBullet=SKAction.removeFromParent()删除
让bulletSequence=SKAction.sequence([bulletSound,moveBullet,deleteBullet])
bullet.runAction(bulletSequence)
}
func{
让randomXStart=random(最小:CGRectGetMinX(游戏区域),最大:CGRectGetMaxX(游戏区域))
设randomXEnd=random(最小:CGRectGetMinX(游戏区域),最大:CGRectGetMaxX(游戏区域))
设startPoint=CGPoint(x:randomXStart,y:self.size.height*1.2)
设端点=CGPoint(x:randomXEnd,y:-self.size.height*0.2)
让敌人=SKSpriteNode(图像名为:“敌人”)
敌人。设置刻度(1)
敌方位置=起始点
敌方位置=2
敌方.physicsBody=SKPhysicsBody(矩形尺寸:敌方.size)
敌人。physicsBody!。受重力影响=false
敌方.physicsBody!.categoryBitMask=physicsCategories.敌方
敌方.physicsBody!.categoryBitMask=physicsCategories.None
敌方.physicsBody!.contactTestBitMask=physicsCategories.Player | physicsCategories.Bullet
自我补充儿童(敌人)
让move敌军=SKAction.moveTo(端点,持续时间:1.5)
让deleteForeign=SKAction.removeFromParent()
让EneySequence=SKAction.sequence([Move敌军,Delete敌军])
敌方行动(敌方顺序)
设dx=endPoint.x-startPoint.x
设dy=endPoint.y-startPoint.y
设amountToRotate=atan2(dy,dx)
敌方旋转=数量旋转
}
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
火弹()
//  GameScene.swift
//  One Mission
//
//  Created by Robert Smith on 7/8/16.
//  Copyright (c) 2016 RobTheMan. All rights reserved.
//

import SpriteKit
import CoreMotion
class GameScene: SKScene, SKPhysicsContactDelegate{


let bulletSound = SKAction.playSoundFileNamed("BulletSound.wav" , waitForCompletion: false)
var motionTimer = NSTimer()
var motionManager: CMMotionManager = CMMotionManager()

struct physicsCategories {
    static let None : UInt32 = 0
    static let Player : UInt32 = 0b1 // 1
    static let Bullet : UInt32 = 0b10 //2
    static let Enemy : UInt32 = 0b100 // 4

}

let player = SKSpriteNode(imageNamed: "playerShip")


func random() -> CGFloat {
    return CGFloat(Float(arc4random()) / 0xFFFFFFFF)

}
func random(min min: CGFloat, max: CGFloat) -> CGFloat{
    return random() * (max - min) + min
}




let gameArea: CGRect

override init(size: CGSize) {

    let maxAspectRatio: CGFloat = 16.0/9.0
    let playableWidth = size.height / maxAspectRatio
    let margin = (size.width - playableWidth) / 2
    gameArea = CGRect(x: margin, y: 0 , width: playableWidth, height: size.height)


    super.init(size: size)

}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}




override func didMoveToView(view: SKView) {
    motionManager.deviceMotionUpdateInterval = 0.01
    motionManager.startDeviceMotionUpdatesToQueue(NSOperationQueue.currentQueue(), withHandler:{
        deviceManager, error in
    })
    motionManager.startAccelerometerUpdates()
    motionTimer = NSTimer.scheduledTimerWithTimeInterval(0.001, target:self, selector: Selector("calculateMotion"), userInfo: nil, repeats: true)
    self.physicsWorld.contactDelegate = self

    let background = SKSpriteNode(imageNamed: "background")
    background.size = self.size
    background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
    background.zPosition = 0
    self.addChild(background)


    player.setScale(1)
    player.position = CGPoint(x: self.size.width/2, y: self.size.height * 0.2)
    player.zPosition = 2
    player.physicsBody = SKPhysicsBody(rectangleOfSize:  player.size)
    player.physicsBody!.affectedByGravity = false
    player.physicsBody!.categoryBitMask = physicsCategories.Player
    player.physicsBody!.collisionBitMask = physicsCategories.None
    player.physicsBody!.contactTestBitMask = physicsCategories.Enemy
    self.addChild(player)

    startNewLevel()

}
func calculateMotion() {
    if let data = motionManager.accelerometerData {
        // you could do (you can change 2 to whatever you want - that makes it x times as fast/sensitive)
        player.position.x+=(CGFloat(2)*CGFloat(data.acceleration.x))
        if data.acceleration.x >= 0.8 || data.acceleration.x <= -0.8{
             //Make whatever you want to happen when there is acceleration happen here
        }
    }
}

func startNewLevel(){


    let spawn = SKAction.runBlock(spawnEnemy)
    let waitToSpawn = SKAction.waitForDuration(1)
    let spawnSequence = SKAction.sequence([spawn, waitToSpawn])
    let spawnForever = SKAction.repeatActionForever(spawnSequence)
    self.runAction(spawnForever)


}


func fireBullet() {

    let bullet = SKSpriteNode(imageNamed: "bullet")
    bullet.setScale(0.8)
    bullet.position = player.position
    bullet.zPosition = 1
    bullet.physicsBody = SKPhysicsBody(rectangleOfSize: bullet.size)
    bullet.physicsBody!.affectedByGravity = false
    bullet.physicsBody!.categoryBitMask = physicsCategories.Bullet
    bullet.physicsBody!.collisionBitMask = physicsCategories.None
    bullet.physicsBody!.contactTestBitMask = physicsCategories.Enemy
    self.addChild(bullet)

    let moveBullet = SKAction.moveToY(self.size.height + bullet.size.height, duration: 1)
    let deleteBullet = SKAction.removeFromParent()
    let bulletSequence = SKAction.sequence([bulletSound, moveBullet, deleteBullet])
    bullet.runAction(bulletSequence)


}


func spawnEnemy(){

    let randomXStart = random(min: CGRectGetMinX(gameArea), max: CGRectGetMaxX(gameArea))
    let randomXEnd = random(min: CGRectGetMinX(gameArea), max: CGRectGetMaxX(gameArea))

    let startPoint = CGPoint(x: randomXStart, y: self.size.height * 1.2)
    let endPoint = CGPoint(x: randomXEnd, y: -self.size.height * 0.2)

    let enemy = SKSpriteNode(imageNamed: "enemyShip")
    enemy.setScale(1)
    enemy.position = startPoint
    enemy.zPosition = 2
    enemy.physicsBody = SKPhysicsBody(rectangleOfSize: enemy.size)
    enemy.physicsBody!.affectedByGravity = false
    enemy.physicsBody!.categoryBitMask = physicsCategories.Enemy
    enemy.physicsBody!.categoryBitMask = physicsCategories.None
    enemy.physicsBody!.contactTestBitMask = physicsCategories.Player | physicsCategories.Bullet
    self.addChild(enemy)

    let moveEnemy = SKAction.moveTo(endPoint, duration: 1.5)

    let deleteEnemy = SKAction.removeFromParent()
    let enemySequence = SKAction.sequence([moveEnemy, deleteEnemy])
    enemy.runAction(enemySequence)

    let dx = endPoint.x - startPoint.x
    let dy = endPoint.y - startPoint.y
    let amountToRotate = atan2(dy, dx)
    enemy.zRotation = amountToRotate



}


override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    fireBullet()
}


override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

    for touch: AnyObject in touches{

        let pointOfTouch = touch.locationInNode(self)
        let previousPointOfTouch = touch.previousLocationInNode(self)

        let amountDragged = pointOfTouch.x - previousPointOfTouch.x

        player.position.x  += amountDragged

        if player.position.x > CGRectGetMaxX(gameArea) - player.size.width / 2{

            player.position.x = CGRectGetMaxX(gameArea) - player.size.width / 2


        }

        if player.position.x < CGRectGetMinX(gameArea) + player.size.width / 2{
            player.position.x = CGRectGetMinX(gameArea) + player.size.width / 2
        }

    }

}


}