我一直在开发一种游戏,其中彩色圆点(SpriteKitShapeNodes)从屏幕顶部落到底部。之前,我正在使用SpriteKit引力来降低点数,但我已经决定要改善下降方面并决定使用脉冲代替。由于一些奇怪的原因,点不接受冲动或其他东西。点根本不动。兴趣点在"变化[颜色]"功能
var Green = SKShapeNode()
var Red = SKShapeNode()
var Yellow = SKShapeNode()
var Purple = SKShapeNode()
var Blue = SKShapeNode()
var Green2 = SKShapeNode()
var Red2 = SKShapeNode()
var Yellow2 = SKShapeNode()
var Purple2 = SKShapeNode()
var Blue2 = SKShapeNode()
//var sparkEmitter = SKEmitterNode(fileNamed: "Animations/Spark")
let userDefaults = NSUserDefaults.standardUserDefaults()
var ScoreLabel:SKLabelNode!
var HighScoreLabel:SKLabelNode!
var RestartLabel:SKLabelNode!
var BackButton:SKSpriteNode!
var π = M_PI
var clickPlayer: AVAudioPlayer = AVAudioPlayer()
var hitPlayer: AVAudioPlayer = AVAudioPlayer()
var jumpPlayer: AVAudioPlayer = AVAudioPlayer()
var musicPlayer: AVAudioPlayer = AVAudioPlayer()
var pointPlayer: AVAudioPlayer = AVAudioPlayer()
var pop2Player: AVAudioPlayer = AVAudioPlayer()
var Background = SKSpriteNode()
var startTimer = NSTimer()
var CustomWidth: UInt32 = UInt32()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
//Add this back in if you want the color needed to change every minute
//var timer = NSTimer.scheduledTimerWithTimeInterval(60, target: self, selector: "update", userInfo: nil, repeats: true)
CustomWidth = UInt32(frame.size.width - 300)
print("Normal Width Is: \(frame.size.width)")
print("Other Width Is: \(UIScreen.mainScreen().bounds.size.width)")
print("Middle is: \(self.size.width / 2)")
self.size.width = UIScreen.mainScreen().bounds.size.width
self.size.height = UIScreen.mainScreen().bounds.size.height
print("Width is now: \(self.size.width)")
Background = SKSpriteNode(imageNamed: "images/edit/background")
Background.position = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height / 2 )
Background.zPosition = -1.0
self.addChild(Background)
//Physics
//self.physicsWorld.gravity = CGVector( dx: 0.0, dy: -0.2 )
//self.physicsWorld.contactDelegate = self
print(physicsWorld.speed)
setupCircles()
spawnCircles()
changeColorNeeded()
func restartGame() {
//self.physicsWorld.speed = 1.0
spawnCircles()
changeColorNeeded()
removeRestartLabel()
addBackScoreLabels()
//self.physicsWorld.gravity = CGVector( dx: 0.0, dy: -0.2 )
}
func setupCircles() {
Green = SKShapeNode(circleOfRadius: radius) // Size of Circle
Green.position.y = frame.size.height
Green.strokeColor = SKColor.clearColor()
Green.glowWidth = 1.0
Green.fillColor = SKColor.greenColor()
Green.physicsBody = SKPhysicsBody(circleOfRadius: radius)
Green.physicsBody?.categoryBitMask = Physics.Enemy
Green.physicsBody?.contactTestBitMask = Physics.Bar
Green.physicsBody?.collisionBitMask = Physics.Bar
Green.physicsBody?.dynamic = true
Green.name = "Enemy"
Red = SKShapeNode(circleOfRadius: radius) // Size of Circle
Red.position.y = frame.size.height
Red.strokeColor = SKColor.clearColor()
Red.glowWidth = 1.0
Red.fillColor = SKColor.redColor()
Red.physicsBody = SKPhysicsBody(circleOfRadius: radius)
Red.physicsBody?.categoryBitMask = Physics.Enemy
Red.physicsBody?.contactTestBitMask = Physics.Bar
Red.physicsBody?.collisionBitMask = Physics.Bar
Red.physicsBody?.dynamic = true
Red.name = "Enemy"
Blue = SKShapeNode(circleOfRadius: radius) // Size of Circle
Blue.position.y = frame.size.height
Blue.strokeColor = SKColor.clearColor()
Blue.glowWidth = 1.0
Blue.fillColor = SKColor.blueColor()
Blue.physicsBody = SKPhysicsBody(circleOfRadius: radius)
Blue.physicsBody?.categoryBitMask = Physics.Enemy
Blue.physicsBody?.contactTestBitMask = Physics.Bar
Blue.physicsBody?.collisionBitMask = Physics.Bar
Blue.physicsBody?.dynamic = true
Blue.name = "Enemy"
Yellow = SKShapeNode(circleOfRadius: radius) // Size of Circle
Yellow.position.y = frame.size.height
Yellow.strokeColor = SKColor.clearColor()
Yellow.glowWidth = 1.0
Yellow.fillColor = SKColor.yellowColor()
Yellow.physicsBody = SKPhysicsBody(circleOfRadius: radius)
Yellow.physicsBody?.categoryBitMask = Physics.Enemy
Yellow.physicsBody?.contactTestBitMask = Physics.Bar
Yellow.physicsBody?.collisionBitMask = Physics.Bar
Yellow.physicsBody?.dynamic = true
Yellow.name = "Enemy"
Purple = SKShapeNode(circleOfRadius: radius) // Size of Circle
Purple.position.y = frame.size.height
Purple.strokeColor = SKColor.clearColor()
Purple.glowWidth = 1.0
Purple.fillColor = SKColor.purpleColor()
Purple.physicsBody = SKPhysicsBody(circleOfRadius: radius)
Purple.physicsBody?.categoryBitMask = Physics.Enemy
Purple.physicsBody?.contactTestBitMask = Physics.Bar
Purple.physicsBody?.collisionBitMask = Physics.Bar
Purple.physicsBody?.dynamic = true
Purple.name = "Enemy"
self.addChild(Green)
self.addChild(Red)
self.addChild(Blue)
self.addChild(Yellow)
self.addChild(Purple)
}
func changeGreen(){
if IsPaused == false{
Green.position.y = frame.size.height + CGFloat(arc4random_uniform(100) + 1)
var PositionX: CGFloat = CGFloat(UInt32(radius) + arc4random_uniform(UInt32(self.frame.width - 2.0 * radius)))
Green.position.x = CGFloat(PositionX)
while Green.containsPoint(Red.position) || Green.containsPoint(Blue.position) || Green.containsPoint(Yellow.position) || Green.containsPoint(Purple.position) {
print("Overlap Detected")
changeGreen()
}
let randomX: CGFloat = CGFloat(drand48())
print("RandomX For Impulse = \(randomX)")
self.Green.physicsBody?.applyImpulse(CGVectorMake(0, 0.1))
}
}
func changeRed(){
if IsPaused == false{
Red.position.y = frame.size.height + CGFloat(arc4random_uniform(100) + 1)
var PositionX: CGFloat = CGFloat(UInt32(radius) + arc4random_uniform(UInt32(self.frame.width - 2.0 * radius)))
Red.position.x = CGFloat(PositionX)
while Red.containsPoint(Blue.position) || Red.containsPoint(Yellow.position) || Red.containsPoint(Green.position) || Red.containsPoint(Purple.position) {
print("Overlap Detected")
changeRed()
}
let randomX: CGFloat = CGFloat(drand48())
print("RandomX For Impulse = \(randomX)")
self.Red.physicsBody?.applyImpulse(CGVectorMake(0, 0.1))
}
}
func changeBlue() {
if IsPaused == false{
Blue.position.y = frame.size.height + CGFloat(arc4random_uniform(100) + 1)
var PositionX: CGFloat = CGFloat(UInt32(radius) + arc4random_uniform(UInt32(self.frame.width - 2.0 * radius)))
Blue.position.x = CGFloat(PositionX)
while Blue.containsPoint(Red.position) || Blue.containsPoint(Yellow.position) || Blue.containsPoint(Green.position) || Blue.containsPoint(Purple.position) {
print("Overlap Detected")
changeBlue()
}
let randomX: CGFloat = CGFloat(drand48())
print("RandomX For Impulse = \(randomX)")
print("Applying Impulse")
self.Blue.physicsBody?.applyImpulse(CGVectorMake(0, 0.1))
}
}
func changeYellow() {
if IsPaused == false{
Yellow.position.y = frame.size.height + CGFloat(arc4random_uniform(100) + 1)
var PositionX: CGFloat = CGFloat(UInt32(radius) + arc4random_uniform(UInt32(self.frame.width - 2.0 * radius)))
Yellow.position.x = CGFloat(PositionX)
while Yellow.containsPoint(Red.position) || Yellow.containsPoint(Blue.position) || Yellow.containsPoint(Green.position) || Yellow.containsPoint(Purple.position) {
print("Overlap Detected")
changeYellow()
}
let randomX: CGFloat = CGFloat(drand48())
print("RandomX For Impulse = \(randomX)")
self.Yellow.physicsBody?.applyImpulse(CGVectorMake(0, 0.1))
}
}
func changePurple() {
if IsPaused == false{
Purple.position.y = frame.size.height + CGFloat(arc4random_uniform(100) + 1)
var PositionX: CGFloat = CGFloat(UInt32(radius) + arc4random_uniform(UInt32(self.frame.width - 2.0 * radius)))
Purple.position.x = CGFloat(PositionX)
while Purple.containsPoint(Red.position) || Purple.containsPoint(Blue.position) || Purple.containsPoint(Yellow.position) || Purple.containsPoint(Green.position) {
print("Overlap Detected")
changePurple()
}
let randomX: CGFloat = CGFloat(drand48())
print("RandomX For Impulse = \(randomX)")
self.Purple.physicsBody?.applyImpulse(CGVectorMake(0, 0.1))
}
}
func spawnCircles() {
changeRed()
changeBlue()
changeGreen()
changeYellow()
changePurple()
}
func randomYPos() -> Double{
var rand = Double(arc4random_uniform(UInt32(0.10) + UInt32(0.8)))
return rand
}
func changeColorNeeded() -> SKColor {
colorNeeded = randomColor()
return self.colorNeeded
}
func randomColor() -> SKColor{
var colors: [SKColor] = [SKColor.yellowColor(), SKColor.blueColor(), SKColor.greenColor(), SKColor.redColor(), SKColor.purpleColor()]
var rand = Int(arc4random_uniform(4) + 0)
return colors[rand]
}
答案 0 :(得分:2)
I cannot see anything obviously wrong with your code. Did you try to increase the value of the impulse?. 0.1 seems quite low. Also if you want them to move down you need to make it a negative value.
It is also a good idea if you follow the swift programming guide. Your properties should not start with capital letters, only classes, protocols and structs should. This makes you code also harder to read on SO because it is marked blue but shouldn't.