- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个简单的游戏作为一个项目,当它与另一个“图像”碰撞时需要更改我的“鸟图像”
现在我已经设置了所有碰撞检测,我只需要将我的“鸟类图像”的纹理更改为我的新图像
关于如何做到这一点的任何提示?我是编程新手,认为这很容易,但显然不是......
我试过改变
var birdTexture = SKTexture(imageNamed: "bird_img_1.png") // global variable
到
birdTexture = SKTexture(imageNamed: "bird_img_dead.png") // this is inside collision detection
游戏场景类:-
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var bg = SKSpriteNode() // Create Background
var bird = SKSpriteNode() // Create User Bird
var scoreLabel = SKLabelNode() // Create Score Label
var score: Int = 0 // Create Score Integer
var enemyBird = SKSpriteNode() // Create Enemy Bird
var birdGroup:UInt32 = 1 // Bird Collision Group
var objectGroup:UInt32 = 2 // Enemy Collision Group
var scoreGroup:UInt32 = 3 // Score Collision Group
var gameOver = 0 // Game Over function
var movingObjects = SKNode() // ??
var scoreTimer = NSTimer() // Create Score Timer
var gameOverLabel = SKLabelNode() // Game over label
var labelHolder = SKSpriteNode() // Holds Label - Game Over
var enemySpawnTimer = NSTimer() // Starts Enemy Spawner
var Menu = 0
/* Put Bird in and animate */
var birdTexture = SKTexture(imageNamed: "bird_img_1.png")
var birdTexture2 = SKTexture(imageNamed: "bird_img_2.png")
var birdTexture3 = SKTexture(imageNamed: "bird_img_3.png")
var birdTexture4 = SKTexture(imageNamed: "bird_img_4.png")
var birdDeadTexture = SKTexture(imageNamed: "bird_img_dead.png")
/* ------------------------------------ Main Setup ------------------------------------------ */
override func didMoveToView(view: SKView) {
/* Setup your scene here */
println("Moved to Game Scene")
movingObjects.speed = 0
/* Background Image setup */
backgroundImage()
/* Set up deletates and physics and timer */
self.physicsWorld.contactDelegate = self
self.physicsWorld.gravity = CGVectorMake(0, -5)
self.addChild(movingObjects)
self.addChild(labelHolder)
/* Load Score Text */
scoreLabel.fontName = "Helvetica"
scoreLabel.fontSize = 60
scoreLabel.text = "0"
scoreLabel.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame)+self.frame.size.height*0.3)
self.addChild(scoreLabel)
bird = SKSpriteNode(texture: birdTexture)
bird.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
// Make bird smaller
bird.size.height = bird.size.width/12
bird.size.width = bird.size.width/12
// Animate Bird
var animation = SKAction.animateWithTextures([birdTexture, birdTexture2, birdTexture3, birdTexture4], timePerFrame: 0.08)
var makeBirdFlap = SKAction.repeatActionForever(animation)
bird.runAction(makeBirdFlap)
// Load Physics
// birdPhysics()
// Run bird animation
bird.zPosition = 10
self.addChild(bird)
/* Introduce ground and top into the scene */
// Create Ground
var ground = SKSpriteNode()
ground.position = CGPointMake(CGRectGetMidX(self.frame), self.frame.size.height/100 * 16)
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, 1))
ground.physicsBody?.dynamic = false
ground.physicsBody?.categoryBitMask = objectGroup
// Add Ground to scene
self.addChild(ground)
// Create Top
var top = SKSpriteNode()
top.position = CGPointMake(0, self.frame.size.height)
top.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, 1))
top.physicsBody?.dynamic = false
top.physicsBody?.categoryBitMask = objectGroup
// Add Top to scene
self.addChild(top)
}
/* ------------------------------------ Spawning Enemy Birds ------------------------------------------ */
/* Create and Spawn enemy birds */
func enemySpawn() {
let height = self.frame.size.height // screen height as variable
let width = self.frame.size.width // screen width as variable
var bottomBarHeight = self.frame.size.height*0.16 // bottom bar height
var spawningPoint = height - bottomBarHeight // screen height - 16%
var randSpawn = arc4random_uniform(UInt32(spawningPoint)) // random number between 0 and 645
var finalSpawn = CGFloat(randSpawn) + bottomBarHeight // add 16% (bottombarheight to the random number
/*
CHECK VALUES OF SPAWN
println("\(height) is the total screen height")
println("\(bottomBarHeight) is the pixels below the ground")
println("\(spawningPoint) is the random range")
println("\(randSpawn) is a random number")
println("\(finalSpawn) - Spawn Location")
println(enemyBird.position)
*/
if (enemyBird.position.y > 730) {
enemyBird.position.y = 720
}
if (enemyBird.position.y < 130) {
enemyBird.position.y = 135
}
// println(Int(enemyBird.position.y))
// Create enemy bird
var enemyBirdTexture = SKTexture(imageNamed: "enemy_img_2.png")
enemyBird = SKSpriteNode(texture: enemyBirdTexture)
enemyBird.position = CGPoint(x: CGRectGetMidX(self.frame)*2.5, y: finalSpawn)
enemyBird.size.height = enemyBird.size.height/12
enemyBird.size.width = enemyBird.size.width/12
enemyBird.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(enemyBird.size.width, enemyBird.size.height))
enemyBird.physicsBody?.dynamic = false
enemyBird.physicsBody?.categoryBitMask = objectGroup
// Move enemy birds
var moveEnemyBird = SKAction.moveByX(-self.frame.size.width * 2, y: 0, duration: NSTimeInterval(self.frame.size.width / 100)) //4.14 seconds on screen
var removeEnemyBird = SKAction.removeFromParent()
var moveAndRemoveEnemyBird = SKAction.sequence([moveEnemyBird, removeEnemyBird])
// Add enemy to scene and move enemy
enemyBird.runAction(moveEnemyBird)
movingObjects.addChild(enemyBird)
}
/* ------------------------------------ Other Parts ------------------------------------------ */
func backgroundImage() {
/* Put Background Image In */
// Create Background Texture
var bgTexture = SKTexture(imageNamed: "bg.png")
// Link bg variable to texture
bg = SKSpriteNode(texture: bgTexture)
// Position thr bg image
bg.position = CGPoint(x: CGRectGetMidX(self.frame), y: self.frame.height/2)
bg.size.height = self.frame.height
// Move background image left
var moveBg = SKAction.moveByX(-bgTexture.size().width, y: 0, duration: 9)
var replaceBg = SKAction.moveByX(bgTexture.size().width, y: 0, duration: 0)
var moveBgForever = SKAction.repeatActionForever(SKAction.sequence([moveBg, replaceBg]))
// Keep world never ending
for var i:CGFloat = 0; i < 3; i++ {
// Position Background
bg = SKSpriteNode(texture: bgTexture)
bg.position = CGPoint(x: bgTexture.size().width/2 + bgTexture.size().width * i, y: CGRectGetMidY(self.frame))
// Stretch background full height of screen
bg.size.height = self.frame.height
// Run the action to move BG
bg.runAction(moveBgForever)
self.addChild(bg)
}
}
func startTimers() {
/* Create Score Timer */
//scoreTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector:Selector("scoreCount"), userInfo: nil, repeats: true)
enemySpawnTimer = NSTimer.scheduledTimerWithTimeInterval(0.75, target: self, selector: Selector("enemySpawn"), userInfo: nil, repeats: true)
}
func birdPhysics() {
/* Give bird physics */
// Give bird physics
bird.physicsBody = SKPhysicsBody(circleOfRadius: bird.size.height/3)
bird.physicsBody?.mass = 0.2
bird.physicsBody?.dynamic = true
bird.physicsBody?.allowsRotation = false
bird.physicsBody?.categoryBitMask = birdGroup
bird.physicsBody?.collisionBitMask = objectGroup
bird.physicsBody?.contactTestBitMask = objectGroup
}
func didBeginContact(contact: SKPhysicsContact) {
if contact.bodyA.categoryBitMask == objectGroup || contact.bodyB.categoryBitMask == objectGroup {
scoreTimer.invalidate()
enemySpawnTimer.invalidate()
bird = SKSpriteNode(texture: birdDeadTexture)
if gameOver == 0 {
movingObjects.speed = 0
gameOver = 1
movingObjects.removeAllChildren()// Remove all enemies
gameOverLabel.fontName = "Helvetica"
gameOverLabel.fontSize = 25
gameOverLabel.text = "Tap to retry!"
gameOverLabel.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame)*1.5)
labelHolder.addChild(gameOverLabel)
gameOverLabel.zPosition = 9
}
}
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
if (Menu == 0) {
movingObjects.speed = 1
birdPhysics()
startTimers()
}
if (gameOver == 0) { // Runs if game is not over
bird.physicsBody?.velocity = CGVectorMake(0, 0)
bird.physicsBody?.applyImpulse(CGVectorMake(0, 80))
Menu = 1 // Number on right is jump height
} else { // Runs if game is over
score = 0 // Score int is 0
scoreLabel.text = "0" // Score Label is 0
bird.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame)) // Position Bird in center
bird.physicsBody?.velocity = CGVectorMake(0,0) // Cannot make bird jump
labelHolder.removeAllChildren() // Removes all labels
gameOver = 0 // Sets game over to 0 so game will run
movingObjects.speed = 1
startTimers()
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
最佳答案
你可以改变 SKSpriteNode
的纹理,比如,
sprite.texture = newTexture
在游戏开始前加载纹理。
var birdAliveTexture = SKTexture(imageNamed: "bird_img_1.png")
var birdDeadTexture = SKTexture(imageNamed: "bird_img_dead.png")
var bird : SKSpriteNode!
加载 Sprite 时,为小鸟 Sprite 命名。
bird = SKSpriteNode(texture: birdAliveTexture)
bird.name = "bird"
在didBeginContact
函数中
func didBeginContact(contact: SKPhysicsContact) {
if contact.bodyA.categoryBitMask == objectGroup || contact.bodyB.categoryBitMask == objectGroup {
scoreTimer.invalidate()
enemySpawnTimer.invalidate()
bird.removeAllActions() //changed
bird.texture = birdDeadTexture // changed
if gameOver == 0 {
movingObjects.speed = 0
gameOver = 1
movingObjects.removeAllChildren()// Remove all enemies
gameOverLabel.fontName = "Helvetica"
gameOverLabel.fontSize = 25
gameOverLabel.text = "Tap to retry!"
gameOverLabel.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame)*1.5)
labelHolder.addChild(gameOverLabel)
gameOverLabel.zPosition = 9
}
}
}
关于ios - 快速检测到碰撞时更改图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28049065/
如何更改循环中变量的名称?比如 number1 、 number2 、 number3 、 number4 ? var array = [2,4,6,8] func ap ( number1: Int
我想设置 View 的背景颜色并在一定延迟后将其更改为另一种颜色。这是我的尝试方式: print("setting color 1") self.view.backgroundColor = UICo
我在使用 express-session 时遇到问题。 session 数据不会在请求之间持续存在。 正如您在下面的代码中看到的那样,/join 路由设置了一些 session 属性,但是当 /sur
我试图从叶渲染器获得一个非常简单的结果,用于快速 Steam 的 for 循环。 我正在上传叶文件 HTML,因为它不接受此处格式正确的代码 - 下面的pizza.swift代码- import
你们中有人有什么好的链接可以与我分享吗?我正在寻找一个 FAST 程序员编辑器,它可以非常快速地打开包含超过 100, 000 行代码的文件?我目前正在使用记事本自动取款机,打开一个 29000 行长
我现在正在处理眼动追踪数据,因此拥有一个巨大的数据集(想想数百万行),因此希望有一种快速的方法来完成此任务。这是它的简化版本。 数据告诉您眼睛在每个时间点正在查看的位置以及我们正在查看的每个文件。 X
我是新手,想为计时器或其他设备选择提示音。 如何打开此列表,以选择其中一种声音? Alert sound list 最佳答案 您将无法在应用中使用系统声音。 但是,您可以包括自己的声音文件,并将其显示
我编写了以下代码来构建具有顺序字符串的数组。 它的工作方式与我预期的一样,但我希望它能更快地运行。有没有更有效的方法在PowerShell中产生我想要的结果? 我是PowerShell的新手,非常感谢
我有一个包含一些非唯一行的矩阵,例如: x 尝试 y <- rle(apply(x, 1, paste, collapse = " ")) # y$lengths is the vector con
我的函数“keyboardWillShown”有问题。所以我想要的是菜单打开时,菜单正好出现在键盘上方。它可以在Iphone 8 plus,8、7、6上完美运行。但是,当我在模拟器上运行Iphone
我正在尝试通过Swift 5中的HTTP get方法从API提取数据。它在启动时成功加载了数据,但是当我刷新页面时,它说“索引超出范围”,这是因为数据是不再会在我的日志中读取,因此索引中没有任何内容。
我想做什么: 从我的数据库中获取时间戳并将其转换为用户的时区。 我的代码: let tryItNow = "\(model.timestampName)" let format = D
给定字体名称和字体大小,如何查找字符串的宽度(CGFloat)? (目标是将UIView的宽度设置为足以容纳字符串的宽度。) 我有两个字符串:一个重复“1”,重复36次,另一个重复“M”,重复36次。
我正在尝试解析此JSON ["Items": ( { AccountBalance = 0; AlphabetType = 3; Description = "\U0631\U
我在UINavigationBar内放置了一个UILabel。 我想根据navigationBar的高度增加该标签的字体大小。当navigationBar很大时,我希望字体大小更大;当滚动并缩小nav
我想将用户输入限制为仅有效数字并使用以下内容: func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, rep
目前我有一个包含超过 100.000 张图像的数据库,它们大小不一或类似,但我想为我的公司制作以下内容: 我插入/上传一张图片,系统返回最有可能相同的图片。我不知道使用什么算法,但它需要快速。我可以预
在我的 swift 项目中,我有一个按钮,我想在标签上打印按下该按钮的时间。 如何解决这个问题? 最佳答案 添加到DHEERAJ的答案中,您只需在func press(sender: UIButton
我必须发表评论,尝试在解析中导入数组。然而,有一个问题。 当我尝试从 Parse 加载数组时,我的输出是 ("Blah","Blah","Blah")这是一个元组...而不是一个数组 TT... 如何
我的应用程序有一个名为 MyDevice 的类,我用它来与硬件通信。该硬件是可选的,实例变量也是可选的: var theDevice:MyDevice = nil 然后,在应用程序中,我必须初始化设备
我是一名优秀的程序员,十分优秀!