- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作自定义 SKSpriteNode,当我逐步调用 createShip 时,类中的 heroShip 常量是正确的,但是当我跳回 gameScene 时,那里的 heroShip 常量没有我在调用 createShip 时分配的属性,我我不确定我做错了什么。我已尝试使用类函数,但使用高度和宽度属性时不起作用。
自定义 SKSpriteNode 类
class hero: SKSpriteNode {
var width: CGFloat = 0.0
var height: CGFloat = 0.0
func createShip() -> SKSpriteNode {
let heroShip = SKSpriteNode(imageNamed: "heroShip")
heroShip.anchorPoint = CGPointMake(1.0, 0.5)
heroShip.physicsBody = SKPhysicsBody(rectangleOfSize: heroShip.size)
heroShip.physicsBody?.usesPreciseCollisionDetection = true
heroShip.zPosition = 1.0
heroShip.physicsBody?.mass = 0.02
heroShip.physicsBody?.dynamic = true
heroShip.physicsBody?.affectedByGravity = false
heroShip.physicsBody?.categoryBitMask = ObjectCategory.collisionHeroCategory.rawValue
heroShip.physicsBody?.contactTestBitMask = ObjectCategory.sceneCategory.rawValue
heroShip.physicsBody?.collisionBitMask = 0x0 | ObjectCategory.sceneCategory.rawValue
//heroShip.position = CGPointMake((scene?.frame.size.width)!/6.0, (scene?.frame.size.height)!/2.0)
heroShip.position = CGPointMake(width, height)
return heroShip
}
}
我的游戏场景
class GameScene: SKScene,SKPhysicsContactDelegate{
let background = SKSpriteNode(imageNamed: "background")
var score:Int = 0
let scoreLabel = SKLabelNode(fontNamed: "Courier")
let MotionManager = CMMotionManager()
var heroShip = hero()
override func didMoveToView(view: SKView) {
heroShip.width = self.size.width/6.0
heroShip.height = self.size.height/2.0
heroShip.createShip()
let enemyShip = SKSpriteNode(imageNamed: "enemyShip")
/* Setup your scene here */
self.physicsWorld.contactDelegate = self
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: CGRectMake(0,heroShip.size.width/1.25,frame.width,frame.height - heroShip.size.width*1.6))
scene?.physicsBody?.contactTestBitMask = ObjectCategory.sceneCategory.rawValue
scene?.physicsBody?.categoryBitMask = ObjectCategory.sceneCategory.rawValue
background.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame))
scoreLabel.fontColor = SKColor.whiteColor()
scoreLabel.text = String(format: "Score: %01u",score)
scoreLabel.position = CGPointMake(frame.size.width/2, frame.size.height - scoreLabel.frame.size.width/1.2)
scoreLabel.zPosition = 1.0
enemyShip.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
enemyShip.zPosition = 1.0
enemyShip.physicsBody = SKPhysicsBody(rectangleOfSize: heroShip.size)
enemyShip.physicsBody?.usesPreciseCollisionDetection = true
enemyShip.physicsBody?.mass = 0.02
enemyShip.physicsBody?.dynamic = true
enemyShip.physicsBody?.affectedByGravity = false
enemyShip.physicsBody?.categoryBitMask = ObjectCategory.collisionEnemyCategory.rawValue
enemyShip.physicsBody?.contactTestBitMask = ObjectCategory.collisionBulletCategory.rawValue
enemyShip.physicsBody?.collisionBitMask = 0x0
self.addChild(enemyShip)
self.addChild(background)
self.addChild(self.heroShip)
self.addChild(scoreLabel)
if MotionManager.accelerometerAvailable{
MotionManager.startAccelerometerUpdates()
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let bullet = SKSpriteNode(imageNamed: "bullet")
bullet.position = CGPointMake(heroShip.position.x, heroShip.position.y)
bullet.zPosition = 1.0
// Add physics body for collision detection
bullet.physicsBody = SKPhysicsBody(rectangleOfSize: bullet.frame.size)
bullet.physicsBody?.dynamic = true
bullet.physicsBody?.affectedByGravity = false
bullet.physicsBody?.categoryBitMask = ObjectCategory.collisionBulletCategory.rawValue
bullet.physicsBody?.contactTestBitMask = ObjectCategory.collisionHeroCategory.rawValue
bullet.physicsBody?.collisionBitMask = 0x0;
let action = SKAction.moveToX(CGRectGetMaxX(self.frame) + bullet.size.width, duration: 0.75)
self.addChild(bullet)
bullet.runAction(action, completion: {
bullet.removeAllActions()
bullet.removeFromParent()
})
}
func didBeginContact(contact: SKPhysicsContact) {
if contact.bodyB.categoryBitMask == ObjectCategory.collisionBulletCategory.rawValue && contact.bodyA.categoryBitMask == ObjectCategory.collisionEnemyCategory.rawValue{
score++
}
}
override func update(currentTime: CFTimeInterval) {
let data = MotionManager.accelerometerData
if data?.acceleration.x == nil{
print("nil")
}
else if fabs((data?.acceleration.x)!) > 0.2 {
heroShip.physicsBody?.applyForce(CGVectorMake(0.0, CGFloat(40 * (data?.acceleration.x)!)))
}
scoreLabel.text = String(format: "Score: %01u",score)
}
}
最佳答案
你跑
heroShip.createShip()
然后永远不要对返回的 SKSpriteNode
做任何事情。据我所知,英雄级是英雄船。对于这个答案的其余部分,我将放弃这个假设。
从 GameScene
的顶部开始,您应该进行一些重构。
var heroShip = hero()
将被替换为使用 SKSpriteNode
结构的 init:
var heroShip = hero(imageNamed: "heroShip")
转到 hero
类,func createShip() -> SKSpriteNode {
应该变成 func createShip() {
。由于您现在已经为节点设置了图像纹理,并且正在使用 hero
类作为节点,因此无需返回 SKSpriteNode
。
删除 let heroShip = SKSpriteNode(imageNamed: "heroShip")
因为 hero
类是将成为我们的heroShip
.
将 heroShip.whatever
变量的所有用法替换为 self.whatever
。最后,删除 return heroShip
。
关于ios - 从 GameScene 设置自定义 SKSpriteNode 的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34885798/
我正在我的 SpriteKit 游戏中构建一个 PauseButton 类,我想在 GameScene 激活时绘制一个标签。但是,我需要在 didMoveToView 之外实例化按钮(就在它之前 -
GameScene.sks 和 GameScene.swift 有问题。我试图为我的项目添加一个标题屏幕,但它从那里走下坡路。标题屏幕工作正常,但问题是当我编辑 Gameviewcontroller.
我正在学习 SpriteKit 并开始了一个使用关卡编辑器的小项目。 但是,由于某些原因,GameScene.swift 和 GameScene.sks 似乎没有关联。下面是 .sks 文件设置的屏幕
我的 xCode 项目有问题。我有一个完整的 gamescene.swift 代码文件(600 行)。但是代码不会运行。当我在模拟器中运行我的游戏时,模拟器会显示标准的 gamescene.sks 颜
好吧,我在 Swift 中创建了一个 sprite kit 游戏,我需要展示一个新的 SKScene OVER(因为在新场景中会有一个清晰的背景,所以你仍然可以看到 GameScene 下面)我原来的
遵循 YouTube 上的 Swift 教程。尝试制作游戏。本教程显示了一个 GameScene.swift 文件和一个 GameScene.sks 文件,而我的没有显示。我错过了什么地方的设置吗?与
我一直在玩弄 SpriteKit,并且对如何从代码驱动它有了相当不错的感觉,但我对 Xcode 6 中包含的关卡编辑器感到非常困惑。 我看过 wwdc 视频(“platforms state of u
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 6 年前。 Improve t
Stackoverflow 新手,如果我发布错误,非常抱歉!谢谢 我的应用程序是一个有时间限制的迷宫游戏,一旦时间达到 0,游戏就会结束并进入游戏结束屏幕(单独的 swift 文件),这由一个“重播”
我已经搜索了几个小时,但没有结果。也许我只是在寻找一个简单的解决方案,但我想知道如何以及在哪里更改启动应用程序时自动启动的场景。在我的项目中制作主菜单或制作人员名单场景。 最佳答案 您需要转到 Vie
在付费网站上的一些教程中,我了解到您可以通过简单地再次呈现相同的场景来重玩游戏,但我无法再访问该教程,所以我不确定我做错了什么。如果我没记错的话,教程使用了类似的内容(在 GameScene 类中):
我的 GameScene 中有我的游戏说明,完成说明后我按下按钮转到 GamePlay 场景,我希望 GameScene 在我再次打开我的应用程序时不再出现。我希望 GamePlay 场景在我下次打开
我的游戏已经准备就绪,我唯一缺少(并想添加)的是向社交媒体分享标准消息的选项。我环顾四周,发现了不同的方法来做到这一点,但当我实现它们时,它们似乎都不起作用。我有一种感觉,我做错了可能很简单! 这是我
我正在使用 swift 和 SpriteKit,我遇到了一个问题,我认为我的 GameScene 比我设备的屏幕尺寸宽。我需要做的是移动图像,然后不允许它移过屏幕边缘。 我现在所处的位置是我的图像将与
所以我已经阅读了几个关于此的问题,但大多数都是在 Objective-C 中,我还没有找到任何直接解决/回答这个问题的问题。我是这里编程的新手,所以请非常彻底地解释任何建议。 我需要了解如何在游戏结束
您好,我的问题只是如何在 GameScene 中取消隐藏按钮。我有一个在 Storyboard中设置的 segueToMainMenu 按钮。这就是我的 GameViewController 的外观:
您好,我的问题是,当我单击按钮重新启动游戏时,它会重新启动,但如果我单击转换到 MainMenuViewController 的按钮,场景就会卡住。有趣的是,如果我重新启动游戏并首先单击segue按钮
我是一个菜鸟,所以,如果这是一个非常基本的问题,请原谅我。我正在创建我的第一个应用程序,它是一个游戏。对于一个文件来说,代码变得太大了(一直上下移动很烦人)。所以我把我的函数放在其他文件中。问题是 a
我正在尝试制作一个简单的 2D 平台游戏。我决定使用 SpriteKit 引擎提供的 Sprite ,而不是导入 Sprite :在 GameScene.sks 中,我拖放了一个 Sprite (简单
各位程序员大家好。这是场景。用户可以在游戏过程中访问 InGameMenuScene,从而允许他们访问设置等。 InGameMenuScene 上的按钮之一 BackToGame 应该带您回到最初所在
我是一名优秀的程序员,十分优秀!