嗨我一直在試圖做很長一段時間,我使用Swift SpriteKit,我的問題是我需要爲一個節點做一個runBlock動作我該如何做運行功能的動作我試過這個:我該如何做一個運行塊動作Swift SpriteKit
var a = SKAction.runBlock({() in self.spawn1()})
對你的信息spawn1是函數。
在此先感謝!
繼承人我的第一個答案的評論幫助代碼:
//
// PlaysScene.swift
// Pocket Rocket3
//
// Created by Lucas Farleigh on 27/11/2014.
// Copyright (c) 2014 Lucas Farleigh. All rights reserved.
//
import spriteKit
class PlayScene:SKScene {
//declaring the node in this scene!
let background = SKSpriteNode(imageNamed: "background")
let bar1 = SKSpriteNode(imageNamed:"bar1")
let bar2 = SKSpriteNode(imageNamed:"bar2")
let bar3 = SKSpriteNode(imageNamed:"bar3")
let bar4 = SKSpriteNode(imageNamed:"bar4")
let bar5 = SKSpriteNode(imageNamed:"bar5")
let bar6a = SKSpriteNode(imageNamed: "bar6")
let bar6b = SKSpriteNode(imageNamed: "bar6")
let bar6c = SKSpriteNode(imageNamed: "bar6")
let bar6d = SKSpriteNode(imageNamed: "bar6")
let bar6e = SKSpriteNode(imageNamed: "bar6")
let bar6f = SKSpriteNode(imageNamed: "bar6")
let bar6g = SKSpriteNode(imageNamed: "bar6")
let bar6h = SKSpriteNode(imageNamed: "bar6")
let bar6i = SKSpriteNode(imageNamed: "bar6")
let bar6j = SKSpriteNode(imageNamed: "bar6")
//making actions
override func didMoveToView(view: SKView) {
var actionmove = SKAction.moveToY(0, duration: 15)
var delay = SKAction.waitForDuration(NSTimeInterval(1.5))
var delchild = SKAction.removeFromParent()
func spawn1(){
bar6a.position = CGPointMake(800 ,CGRectGetMaxY(self.frame))
bar6b.position = CGPointMake(1600,CGRectGetMaxY(self.frame))
addChild(bar6a)
addChild(bar6b)
}
func spawn2(){
bar6c.position = CGPointMake(400 ,CGRectGetMaxY(self.frame))
bar6d.position = CGPointMake(1200,CGRectGetMaxY(self.frame))
addChild(bar6c)
addChild(bar6d)
}
func spawn3(){
bar6e.position = CGPointMake(600 ,CGRectGetMaxY(self.frame))
bar6f.position = CGPointMake(1400,CGRectGetMaxY(self.frame))
addChild(bar6e)
addChild(bar6f)
}
func spawn4(){
bar6g.position = CGPointMake(700 ,CGRectGetMaxY(self.frame))
bar6h.position = CGPointMake(1500,CGRectGetMaxY(self.frame))
addChild(bar6g)
addChild(bar6h)
}
func spawn5(){
bar6i.position = CGPointMake(700 ,CGRectGetMaxY(self.frame))
bar6j.position = CGPointMake(1500,CGRectGetMaxY(self.frame))
addChild(bar6i)
addChild(bar6j)
}
var a = SKAction.runBlock({ self.spawn1() })
var b = SKAction.runBlock(spawn2)
var c = SKAction.runBlock(spawn3)
var d = SKAction.runBlock(spawn4)
var e = SKAction.runBlock(spawn5)
var delayA = SKAction.waitForDuration(NSTimeInterval(1.5))
var delayB = SKAction.waitForDuration(NSTimeInterval(2))
var delayC = SKAction.waitForDuration(NSTimeInterval(4))
var delayD = SKAction.waitForDuration(NSTimeInterval(6))
var delayE = SKAction.waitForDuration(NSTimeInterval(8))
var sequence1 = SKAction.sequence([delayA,a,actionmove,delchild])
var sequence2 = SKAction.sequence([delayB,b,actionmove,delchild])
var sequence3 = SKAction.sequence([delayC,c,actionmove,delchild])
var sequence4 = SKAction.sequence([delayD,d,actionmove,delchild])
var sequence5 = SKAction.sequence([delayE,e,actionmove,delchild])
var repeat1 = SKAction.repeatActionForever(sequence1)
var repeat2 = SKAction.repeatActionForever(sequence2)
var repeat3 = SKAction.repeatActionForever(sequence3)
var repeat4 = SKAction.repeatActionForever(sequence4)
var repeat5 = SKAction.repeatActionForever(sequence5)
//actionmove: making it smooth
actionmove.timingMode = SKActionTimingMode.EaseOut
//doing stuff with the background
background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
background.yScale = 10.0
background.xScale = 10.0
addChild(background)
//running the actions
bar6a.runAction(repeat1)
bar6b.runAction(repeat1)
bar6c.runAction(repeat2)
bar6d.runAction(repeat2)
bar6e.runAction(repeat3)
bar6f.runAction(repeat3)
bar6g.runAction(repeat4)
bar6h.runAction(repeat4)
bar6i.runAction(repeat5)
bar6j.runAction(repeat5)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
}
}
你把所有的功能
* *裏面的didMoveToView功能,因此它們不是通過書面self.spawn1訪問()因爲它們不是類的函數,而是隻能從didMoveToView中訪問的本地函數。如果這不是問題發佈的錯誤信息(S)你得到。 – LearnCocos2D
等我有一個問題,當我將它們移出didMoveToView()並運行它到應用程序委託,並說線程1:信號SAGRBT –