我正在尝试制作一款泡泡射击游戏,但我无法移动我的划桨。知道为什么会这样吗?
//
// GameScene.swift
// gggame
//
// Created by divy on 6/17/15.
// Copyright (c) 2015 divy. All rights reserved.
//
import SpriteKit
class GameScene: SKScene , SKPhysicsContactDelegate {
var istouchingpaddle = false
let ballcat: UInt32 = 0x1 << 0
let paddlecat : UInt32 = 0x1 << 1
override func didMoveToView(view: SKView) {
let border = SKPhysicsBody(edgeLoopFromRect: self.frame)
border.friction = 0
self.physicsBody = border
self.physicsWorld.gravity = (CGVectorMake(0,-9.8))
self.physicsWorld.contactDelegate = self
//ball setting
let ball = childNodeWithName("ball") as SKSpriteNode
ball.physicsBody?.applyImpulse(CGVectorMake(30, -30))
ball.physicsBody?.allowsRotation = false
ball.physicsBody?.friction = 0
ball.physicsBody?.restitution = 1
ball.physicsBody?.angularDamping = 0
ball.physicsBody?.linearDamping = 0
ball.physicsBody?.categoryBitMask = ballcat
}
func didBeginContact(contact: SKPhysicsContact) {
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
//touch setting
var touch = touches.anyObject() as UITouch!
var location = touch.locationInNode(self)
if let body = self.physicsWorld.bodyAtPoint(location)
{
if body.node!.name == "paddle"
{
istouchingpaddle = true
}
}
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
istouchingpaddle = false
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
if istouchingpaddle{
var touch = touches.anyObject() as UITouch!
var currentlocation = touch.locationInNode(self)
var prevlocation = touch.locationInNode(self)
var paddle = childNodeWithName("paddle") as SKSpriteNode
var xpos = paddle.position.x + (currentlocation.x - prevlocation.x)
xpos = max(xpos, paddle.size.width/2)
xpos = min(xpos, size.width - paddle.size.width/2)
paddle.position = CGPointMake(xpos, paddle.position.y)
}
}
override func update(currentTime: CFTimeInterval) {
}
}
答案 0 :(得分:0)
我无法看到你的代码中为paddle和ball创建了物理实体。所以,你实际上是在物理体存在之前尝试使用它们。这是一个工作示例,为您提供有关如何创建物理实体以及如何移动球拍的基本概念(您可以根据需要进行调整):
//
// GameScene.swift
// gggame
//
// Created by divy on 6/17/15.
// Copyright (c) 2015 divy. All rights reserved.
//
import SpriteKit
class GameScene: SKScene , SKPhysicsContactDelegate {
var istouchingpaddle = false
let ballcat: UInt32 = 0x1 << 0
let ball = SKShapeNode(circleOfRadius: 20) //change this to create a ball from image, because using SKShapeNode can be inefficient
let paddle = SKSpriteNode(color: SKColor.grayColor(), size: CGSize(width: 100.0,height: 30.0))
let paddlecat : UInt32 = 0x1 << 1
override func didMoveToView(view: SKView) {
self.physicsWorld.contactDelegate = self
let border = SKPhysicsBody(edgeLoopFromRect: self.frame)
border.friction = 0
self.physicsBody = border
//ball setting
ball.name = "ball"
ball.position = CGPoint(x: CGRectGetMidX(self.frame),y: CGRectGetMidY(self.frame))
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.size.width/2)
ball.physicsBody?.allowsRotation = false
ball.physicsBody?.affectedByGravity = true
ball.physicsBody?.dynamic = true
ball.physicsBody?.restitution = 1
ball.physicsBody?.categoryBitMask = ballcat
ball.physicsBody?.collisionBitMask = paddlecat
ball.physicsBody?.contactTestBitMask = paddlecat
paddle.name = "paddle"
paddle.position = CGPoint(x: CGRectGetMidX(self.frame) ,y: CGRectGetMinY(self.frame)+100.0)
paddle.physicsBody = SKPhysicsBody(rectangleOfSize: paddle.size)
paddle.physicsBody?.allowsRotation = false
paddle.physicsBody?.dynamic = false
paddle.physicsBody?.affectedByGravity = false
paddle.physicsBody?.restitution = 1
paddle.physicsBody?.categoryBitMask = paddlecat
paddle.physicsBody?.collisionBitMask = ballcat
paddle.physicsBody?.contactTestBitMask = ballcat
self.addChild(ball)
self.addChild(paddle)
}
func didBeginContact(contact: SKPhysicsContact) {
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
//touch setting
var touch = touches.anyObject() as UITouch!
var location = touch.locationInNode(self)
if let body = self.physicsWorld.bodyAtPoint(location)
{
if body.node!.name == "paddle"
{
istouchingpaddle = true
}
}
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
istouchingpaddle = false
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
if istouchingpaddle{
var touch = touches.anyObject() as UITouch!
var currentlocation = touch.locationInNode(self)
paddle.position = CGPointMake(currentlocation.x, paddle.position.y)
}
}
}