我试图找出如何使这项工作。
import Foundation
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var movingGround: PPMovingGround!
var hero: PPHero!
var wallGen: PPWallGen!
var isStarted = false
var isGameOver = false
override func didMoveToView(view: SKView) {
//backgroundColor = UIColor.greenColor()
backgroundColor = UIColor(red: 223/255.0, green: 86/255.0, blue: 94/255.0, alpha: 1.0)
addMovingGround()
addHero()
addWallGen()
addTapToStartLabel()
addStageLabel()
addPointsLabels()
addPhysicsWorld()
loadHighscore()
}
func addLevelLabel() {
}
func addMovingGround() {
movingGround = PPMovingGround(size: CGSizeMake(view!.frame.width, kMLGroundHeight))
movingGround.position = CGPointMake(0, view!.frame.size.height/2)
addChild(movingGround)
}
func addHero() {
hero = PPHero()
hero.position = CGPointMake(70, movingGround.position.y + movingGround.frame.size.height/2 + hero.frame.size.height/2)
addChild(hero)
}
func addWallGen() {
wallGen = PPWallGen(color: UIColor.clearColor(), size: view!.frame.size)
wallGen.position = view!.center
addChild(wallGen)
}
func addTapToStartLabel() {
let tapToStartLabel = SKLabelNode(text: "Tap to start!")
tapToStartLabel.name = "tapToStartLabel"
tapToStartLabel.position.x = view!.center.x
tapToStartLabel.position.y = view!.center.y + 40
tapToStartLabel.fontName = "Helvetica"
tapToStartLabel.fontColor = UIColor.whiteColor()
tapToStartLabel.fontSize = 22.0
addChild(tapToStartLabel)
tapToStartLabel.runAction(blinkAnimation())
}
func addStageLabel() {
let stageLabel = PPStageLabel(num: 1)
stageLabel.name = "stageLabel"
stageLabel.position.x = view!.center.x
stageLabel.position.y = view!.center.y - 120
stageLabel.fontColor = UIColor.whiteColor()
stageLabel.fontName = "Helvetica"
stageLabel.fontSize = 40
addChild(stageLabel)
let stageTextLabel = SKLabelNode(text: "Stage")
stageTextLabel.fontColor = UIColor.whiteColor()
stageTextLabel.fontSize = 14.0
stageTextLabel.fontName = "Helvetica"
stageTextLabel.position = CGPointMake(3.0,-15.0)
stageLabel.addChild(stageTextLabel) }
func addPointsLabels() {
let pointsLabel = PPPointsLabel(num: 0)
pointsLabel.name = "pointsLabel"
pointsLabel.position.x = view!.center.x
pointsLabel.position.y = view!.center.y + 120
pointsLabel.fontColor = UIColor.whiteColor()
pointsLabel.fontName = "Helvetica"
pointsLabel.fontSize = 40
addChild(pointsLabel)
let highscoreLabel = PPPointsLabel(num: 0)
highscoreLabel.name = "highscoreLabel"
highscoreLabel.position = CGPointMake(view!.frame.size.width - 40, view!.frame.size.height - 30)
highscoreLabel.fontColor = UIColor.whiteColor()
highscoreLabel.fontName = "Helvetica"
highscoreLabel.fontSize = 24
addChild(highscoreLabel)
let highscoreTextLabel = SKLabelNode(text: "Highscore: ")
highscoreTextLabel.fontColor = UIColor.whiteColor()
highscoreTextLabel.fontSize = 14.0
highscoreTextLabel.fontName = "Helvetica"
highscoreTextLabel.position = CGPointMake(-70.0,3.5)
highscoreLabel.addChild(highscoreTextLabel)
}
func addPhysicsWorld() {
physicsWorld.contactDelegate = self
}
func loadHighscore() {
let defaults = NSUserDefaults.standardUserDefaults()
let highscoreLabel = childNodeWithName("highscoreLabel") as! PPPointsLabel
highscoreLabel.setTo(defaults.integerForKey("highscore"))
}
// MARK - Game Lifecycle
func start() {
isStarted = true
let tapToStartLabel = childNodeWithName("tapToStartLabel")
tapToStartLabel?.removeFromParent()
hero.stop()
movingGround.start()
wallGen.startGenWallsEvery(1)
}
func gameOver() {
isGameOver = true
// everything stops
hero.fall()
wallGen.stopWalls()
movingGround.stop()
hero.stop()
// create game over label
let gameOverLabel = SKLabelNode(text: "Game Over!")
gameOverLabel.fontColor = UIColor.whiteColor()
gameOverLabel.fontName = "Helvetica"
gameOverLabel.position.x = view!.center.x
gameOverLabel.position.y = view!.center.y + 80
gameOverLabel.fontSize = 22.0
addChild(gameOverLabel)
gameOverLabel.runAction(blinkAnimation())
// save current points label value
let pointsLabel = childNodeWithName("pointsLabel") as! PPPointsLabel
let highscoreLabel = childNodeWithName("highscoreLabel") as! PPPointsLabel
let stageLabel = childNodeWithName("stageLabel") as! PPStageLabel
if highscoreLabel.number < pointsLabel.number {
highscoreLabel.setTo(pointsLabel.number)
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setInteger(highscoreLabel.number, forKey: "highscore")
}
}
func restart() {
let newScence = GameScene(size: view!.bounds.size)
newScence.scaleMode = .AspectFill
view!.presentScene(newScence)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
if isGameOver {
restart()
}else if !isStarted {
start()
}else{
hero.flip()
}
}
override func update(currentTime: CFTimeInterval) {
if wallGen.wallTrackers.count > 0 {
let wall = wallGen.wallTrackers[0] as PPWall
let wallLocation = wallGen.convertPoint(wall.position, toNode: self)
if wallLocation.x < hero.position.x {
wallGen.wallTrackers.removeAtIndex(0)
let pointsLabel = childNodeWithName("pointsLabel") as! PPPointsLabel
pointsLabel.increment()
}
}else if
wallGen.wallTrackers.count > 0 {
let wall = wallGen.wallTrackers[0] as PPWall
let wallLocation = wallGen.convertPoint(wall.position, toNode: self)
if wallLocation.x < hero.position.x {
wallGen.wallTrackers.removeAtIndex(0)
let stageLabel = childNodeWithName("stageLabel") as! PPStageLabel
stageLabel.increment()
}
}
}
// MARK: - SKPhysicsContactDelegate
func didBeginContact(contact: SKPhysicsContact) {
if !isGameOver {
gameOver()
}
}
// MARR: - Animations
func blinkAnimation() -> SKAction {
let duration = 0.4
let fadeOut = SKAction.fadeAlphaTo(0.0, duration: duration)
let fadeIn = SKAction.fadeAlphaTo(1.0, duration: duration)
let blink = SKAction.sequence([fadeOut, fadeIn])
return SKAction.repeatActionForever(blink)
}
}
HTML
function foo(a) {
$('#this-button').show();
}
function buttonClicked() {
//get access to var a
}
这是我所拥有的简化版本,但想法是一样的。
foo接受一个变量,然后使一个按钮可见。单击按钮时,我想用var a。
做更多事情那么请等到单击按钮继续功能?
似乎无法弄明白。
由于
答案 0 :(得分:2)
使用jQuery绑定点击处理程序。您可以使用jQuery.Proxy绑定a
作为参数:
function foo(a) {
$('#this-button').show().click( $.proxy( buttonClicked, null, a ) );
}
function buttonClicked(a) {
// Use a here
}
并从您的html属性中删除JavaScript:
<button id="this-button" />
编辑,如果你想要做的就是在点击按钮后执行一些代码,你可以这样做:
function foo(a) {
// Code up here executes before the button is clicked
$('#this-button').show().unbind( 'click.foo' ).one( 'click.foo', function ( ) {
console.log( a );
// This code executes after the click, and has access to a
} );
// Code down here executes before the button is clicked
}
答案 1 :(得分:1)
您使用event handler content attribute。有权访问:
因此,您可以将变量添加为元素的属性:
function foo(a) {
$('#this-button').show().prop('myProp', a);
}
function buttonClicked(a) {
alert(a);
}
foo(123);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="this-button" onclick="buttonClicked(myProp)">Click me</button>
&#13;
当然,使用event handler IDL attribute或事件监听器会更好。
答案 2 :(得分:0)
这就是全局的原因,或者创建一个具有可以使用闭包的函数的对象。
答案 3 :(得分:0)
您始终可以访问全局范围:
else if (is.complex(x)) {
y <- Im(x)
x <- Re(x)
xlab <- paste0("Re(", ylab, ")")
ylab <- paste0("Im(", ylab, ")")
}
else if (is.matrix(x) || is.data.frame(x)) {
但这通常是不好的做法。你可以重组代码,以便两个地方都可用。
即
window.a = a;
答案 4 :(得分:0)
HTML
<button id="this-button">
JS
function foo(a) {
$('#this-button').show();
$('#this-button').click(buttonClicked);
function buttonClicked() {
//a can be accesed here
}
}
将buttonClicked方法放在foo中以获取变量a
答案 5 :(得分:0)
有几种不同的方法可以为这只猫设置皮肤,但有一种方法是使用闭包来捕获a
变量:
var myButton = document.getElementById('this-button');
function foo(a) {
myButton.addEventListener("click", buttonClicked(a));
...
}
function buttonClicked(a) {
return function() {
console.log('buttonClicked', a);
}
}
foo('Success!');
在这种情况下,函数buttonClicked
返回一个函数,该函数在a
函数运行时捕获foo
的值。然后,此结果函数将传递给event handler
并在触发时运行。
请参阅此处的小提琴:https://jsfiddle.net/ToddT/ae5h1src/
答案 6 :(得分:0)
您可以使用HTML5 localstorage
...
function foo(a) {
$('#this-button').show();
localStorage.setItem("variable_a", a); // variable in localstorage =variable_a
}
function buttonClicked() {
localStorage.getItem('variable_a');
//get access to var a
}
HTML5 localstorage允许您在客户端浏览器上存储数据,您可以通过getItem()
访问它... ...更多信息:[w3schools],[jenkov.com]
答案 7 :(得分:0)
使用闭包。
(function(){
var dummy_a;
function foo(a) {
//$('#this-button').show();
dummy_a = a;
}
function buttonClicked() {
//get access to var a
alert(dummy_a)
}
foo(2)
buttonClicked()
})();
&#13;