私は画面の上部から四角いドロップをしようとしていました。しかし、それが半分になると画面からはね返ります。私は画面の中央にオブジェクトを作成しませんでした。なぜスクリーンの中央でバウンスしているのか分かりません。ここでSKSpriteNodeはSwiftで作成しなかったもののバウンス
は、コードを実行している私のビデオへのリンクです:http://sendvid.com/enm9l1np
ここでは、コードです:
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var animator : UIDynamicAnimator?
let BlueSquare = SKSpriteNode()
let RedSquare = SKSpriteNode()
var scorenumber = 0
var Ground = SKSpriteNode()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
physicsWorld.gravity = CGVector(dx: 1.0, dy: -9.0)
let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
// 2. Set the friction of that physicsBody to 0
borderBody.friction = 0
// 3. Set physicsBody of scene to borderBody
self.physicsBody = borderBody
let width = UInt32(self.frame.size.width)
let X = arc4random() % width
Ground = SKSpriteNode(imageNamed: "Ground")
Ground.setScale(1.0)
Ground.position = CGPoint(x: self.frame.width/2, y: 5)
Ground.physicsBody = SKPhysicsBody(rectangleOfSize: Ground.size)
Ground.physicsBody!.affectedByGravity = false
Ground.physicsBody!.dynamic = false
Ground.zPosition = 3
self.addChild(Ground)
//INizialize
animator = UIDynamicAnimator(referenceView: self.view!)
//Add Gravity
//animator?.addBehavior(gravity, sprite)
BlueSquare.size = CGSize(width: 100, height: 100)
BlueSquare.position = CGPoint(x: CGFloat(X), y: 1000)
BlueSquare.physicsBody?.affectedByGravity = true
BlueSquare.physicsBody?.dynamic = false
BlueSquare.name = "bluecube"
BlueSquare.physicsBody = SKPhysicsBody(circleOfRadius: 20)
BlueSquare.color = SKColor.blueColor()
// BlueSquare.physicsBody?.velocity = CGVectorMake(0, 0)
//BlueSquare.physicsBody?.applyImpulse(CGVectorMake(0, -90))
self.addChild(BlueSquare)
let X2 = arc4random() % width
RedSquare.size = CGSize(width: 100, height: 100)
RedSquare.position = CGPoint(x: CGFloat(X2), y: 1000)
RedSquare.physicsBody?.affectedByGravity = true
RedSquare.physicsBody?.dynamic = true
RedSquare.name = "redcube"
RedSquare.physicsBody = SKPhysicsBody(circleOfRadius: 20)
RedSquare.hidden = false
RedSquare.color = SKColor.redColor()
self.addChild(RedSquare)
var gameTimer = NSTimer!()
gameTimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "spawning1", userInfo: nil, repeats: true)
}
func spawning1() {
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
// let location = touch.locationInNode(self)
let currentPoint = touch.locationInNode(self)
let currentNode = nodeAtPoint(currentPoint)
let nodeName: String? = currentNode.name
if nodeName == "bluecube" {
BlueSquare.hidden = true
score()
}
if nodeName == "redcube" {
RedSquare.hidden = true
score()
}
}
}
func score() {
scorenumber++
print(scorenumber)
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
if CGRectIntersectsRect(RedSquare.frame, Ground.frame) {
RedSquare.position = CGPoint(x: RedSquare.position.x, y: RedSquare.position.y + 10)
}
}
}
大文字で始まる変数名は使用しないでください。変数名(およびメソッド名)は 'camelCase'、クラス名は 'CamelCase'です。変数名はクラス名であると考えているため、フォーマッタは変数名を青で書いていることがわかります。 – Hamish