Urgent help with swift xcode code is needed. Here is my flappy bird code but the
ID: 3772321 • Letter: U
Question
Urgent help with swift xcode code is needed. Here is my flappy bird code but the pipes don't load on the bottom and the score label is not incrementing when the bird passes through. Cab someone help me? Here is my code
import SpriteKit
struct PhysicsCatagory {
static let HeadGroup : UInt32 = 0x1 << 1
static let spacegroup : UInt32 = 0x1 << 2
static let objectGroup : UInt32 = 0x1 << 3
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var score = 0
var scoreLabel = SKLabelNode()
var Ground = SKSpriteNode()
var Head = SKSpriteNode()
var bg = SKSpriteNode()
var movingObjects = SKNode()
var labelHolder = SKSpriteNode()
var gameOver = 0
var pipe2texture = SKTexture()
var pipetexture = SKTexture()
var gameOverLabel = SKLabelNode()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.physicsWorld.contactDelegate = self
self.physicsWorld.gravity = CGVectorMake(0, -5)
self.addChild(movingObjects)
makeBackGround()
Ground = SKSpriteNode(imageNamed: "Ground")
Ground.setScale(0.5)
Ground.position = CGPoint(x: self.frame.width / 2, y: 0 + Ground.frame.height / 2)
Ground.physicsBody = SKPhysicsBody(rectangleOfSize: Ground.size)
Ground.physicsBody?.categoryBitMask = PhysicsCatagory.spacegroup
Ground.physicsBody?.collisionBitMask = PhysicsCatagory.HeadGroup
Ground.physicsBody?.contactTestBitMask = PhysicsCatagory.HeadGroup
Ground.physicsBody?.affectedByGravity = false
Ground.physicsBody?.dynamic = false
Ground.zPosition = 3
self.addChild(Ground)
NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: Selector("makePipes"), userInfo: nil, repeats: true)
Head = SKSpriteNode(imageNamed: "Head")
Head.size = CGSize(width: 60, height: 70)
Head.position = CGPoint(x: self.frame.width / 2 - Head.frame.width, y: self.frame.height / 2)
Head.physicsBody = SKPhysicsBody(circleOfRadius: Head.frame.height / 2)
Head.physicsBody?.categoryBitMask = PhysicsCatagory.HeadGroup
Head.physicsBody?.collisionBitMask = PhysicsCatagory.spacegroup | PhysicsCatagory.objectGroup
Head.physicsBody?.contactTestBitMask = PhysicsCatagory.spacegroup | PhysicsCatagory.objectGroup
Head.physicsBody?.affectedByGravity = true
Head.physicsBody?.dynamic = true
Head.zPosition = 2
self.addChild(Head)
scoreLabel.fontName = "Helvetica"
scoreLabel.fontSize = 60
scoreLabel.text = "0"
scoreLabel.position = CGPointMake(CGRectGetMidX(self.frame), self.frame.size.height - 70)
scoreLabel.zPosition = 3
self.addChild(scoreLabel)
makePipes()
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if gameOver == 0 {
Head.physicsBody?.velocity = CGVectorMake(0, 0)
Head.physicsBody?.applyImpulse(CGVectorMake(0, 90))
}
else {
score = 0
scoreLabel.text = "0"
movingObjects.removeAllChildren()
Head.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
Head.physicsBody?.velocity = CGVectorMake(0, 0)
labelHolder.removeAllChildren()
gameOver = 0
movingObjects.speed = 1
}
}
func createWalls(){
let wallPair = SKNode()
let topWall = SKSpriteNode(imageNamed: "Wall")
let btmWall = SKSpriteNode(imageNamed: "Wall")
topWall.position = CGPoint(x: self.frame.width / 1.5, y: self.frame.height / 2 + 250)
btmWall.position = CGPoint(x: self.frame.width / 1.5, y: self.frame.height / 2 - 250)
topWall.setScale(0.5)
btmWall.setScale(0.5)
topWall.physicsBody = SKPhysicsBody(rectangleOfSize: topWall.size)
topWall.physicsBody?.categoryBitMask = PhysicsCatagory.objectGroup
topWall.physicsBody?.collisionBitMask = PhysicsCatagory.HeadGroup
topWall.physicsBody?.contactTestBitMask = PhysicsCatagory.HeadGroup
topWall.physicsBody?.dynamic = false
topWall.physicsBody?.affectedByGravity = false
btmWall.physicsBody = SKPhysicsBody(rectangleOfSize: btmWall.size)
btmWall.physicsBody?.categoryBitMask = PhysicsCatagory.objectGroup
btmWall.physicsBody?.collisionBitMask = PhysicsCatagory.HeadGroup
btmWall.physicsBody?.contactTestBitMask = PhysicsCatagory.HeadGroup
btmWall.physicsBody?.dynamic = false
btmWall.physicsBody?.affectedByGravity = false
topWall.zRotation = CGFloat(M_PI)
wallPair.addChild(topWall)
wallPair.addChild(btmWall)
wallPair.zPosition = 1
self.addChild(wallPair)
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
func makeBackGround(){
let bgTexture = SKTexture(imageNamed: "bg")
let movebg = SKAction.moveByX(-bgTexture.size().width, y: 0, duration: 9)
let replacebg = SKAction.moveByX(bgTexture.size().width, y: 0, duration: 0)
let movebgforever = SKAction.repeatActionForever(SKAction.sequence([movebg, replacebg]))
for var i: CGFloat = 0; i < 5; ++i{
bg = SKSpriteNode(texture: bgTexture)
bg.position = CGPoint(x: bgTexture.size().width/2 + bgTexture.size().width * i, y: CGRectGetMidY(self.frame))
bg.size.height = self.frame.height
bg.runAction(movebgforever)
movingObjects.addChild(bg)
}
}
func makePipes(){
if gameOver == 0{
print("Enter the makePipes")
let gapheight = Head.size.height * 6
let movementAmount = arc4random() % UInt32(self.frame.height/2)
let pipeoffset = CGFloat(movementAmount) - self.frame.size.height/4
let movepipes = SKAction.moveByX(-self.frame.size.width * 2, y: 0, duration: NSTimeInterval(self.frame.size.width/100))
let removepipes = SKAction.removeFromParent()
let moveandremovepipes = SKAction.sequence([movepipes, removepipes])
let topWall = SKSpriteNode(imageNamed: "Wall")
let btmWall = SKSpriteNode(imageNamed: "Wall")
topWall.setScale(0.5)
btmWall.setScale(0.5)
topWall.runAction(moveandremovepipes)
topWall.position = CGPoint(x: CGRectGetMidX(self.frame) + self.frame.size.width, y: CGRectGetMidY(self.frame) + topWall.size.height/2 + gapheight/2 + pipeoffset)
topWall.physicsBody = SKPhysicsBody(rectangleOfSize: topWall.size)
topWall.physicsBody?.dynamic = false
topWall.physicsBody?.allowsRotation = false
topWall.physicsBody?.categoryBitMask = PhysicsCatagory.objectGroup
topWall.zRotation = CGFloat(M_PI)
movingObjects.addChild(topWall)
btmWall.runAction(moveandremovepipes)
btmWall.position = CGPoint(x: CGRectGetMidX(self.frame) + self.frame.size.width, y: CGRectGetMidY(self.frame) + btmWall.size.height/2 + gapheight/2 + pipeoffset)
btmWall.physicsBody = SKPhysicsBody(rectangleOfSize: btmWall.size)
btmWall.physicsBody?.dynamic = false
btmWall.physicsBody?.allowsRotation = false
btmWall.physicsBody?.categoryBitMask = PhysicsCatagory.objectGroup
btmWall.zRotation = CGFloat(M_PI)
movingObjects.addChild(btmWall)
let space = SKNode()
space.position = CGPoint(x: CGRectGetMidX(self.frame) + self.frame.size.width, y: CGRectGetMidY(self.frame) + pipeoffset)
space.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(topWall.size.width, gapheight))
space.runAction(moveandremovepipes)
space.physicsBody?.dynamic = false
space.physicsBody?.allowsRotation = false
space.physicsBody?.categoryBitMask = PhysicsCatagory.spacegroup
space.physicsBody?.collisionBitMask = PhysicsCatagory.spacegroup
space.physicsBody?.contactTestBitMask = PhysicsCatagory.HeadGroup
movingObjects.addChild(space)
topWall.zPosition = 2
btmWall.zPosition = 2
}
func didBeginContact(contact: SKPhysicsContact) {
if contact.bodyA.categoryBitMask == PhysicsCatagory.spacegroup || contact.bodyB.categoryBitMask == PhysicsCatagory.spacegroup {
score++
scoreLabel.text = "(score)"
} else {
if gameOver == 0{
gameOver = 1
movingObjects.speed = 0
gameOverLabel.fontName = "Helvetica"
gameOverLabel.text = "GameOver!"
gameOverLabel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
labelHolder.addChild(gameOverLabel)
}
}
}
}
}
Explanation / Answer
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var score = 0
var gameOver = false
var gameOverLabel = SKLabelNode()
var scoreLabel = SKLabelNode ()
var bird = SKSpriteNode()
var bg = SKSpriteNode()
var movingObjects = SKSpriteNode()
var labelContainer = SKSpriteNode()
var pipe1 = SKSpriteNode()
var pipe2 = SKSpriteNode()
enum ColliderType: UInt32 {
case Bird = 1
case Object = 2
case Gap = 4
}
func makeBg () {
let bgTexture = SKTexture(imageNamed: "bg.png")
let movebg = SKAction.moveByX(-bgTexture.size().width, y: 0, duration: 10)
let replacebg = SKAction.moveByX(bgTexture.size().width, y: 0, duration: 0)
let movebgForever = SKAction.repeatActionForever(SKAction.sequence([movebg,replacebg]))
for var i: CGFloat = 0; i<2; i++ {
bg = SKSpriteNode(texture: bgTexture)
bg.position = CGPoint(x: bgTexture.size().width / 2 + bgTexture.size().width * i, y: CGRectGetMidY(self.frame))
bg.zPosition = -5
bg.size.height = self.frame.height
bg.runAction(movebgForever)
movingObjects.addChild(bg)
}
}
override func didMoveToView(view: SKView) {
self.physicsWorld.contactDelegate = self
self.addChild(movingObjects)
self.addChild(labelContainer)
makeBg()
scoreLabel.fontName = "Helvetica"
scoreLabel.fontSize = 60
scoreLabel.text = "0"
scoreLabel.position = CGPointMake(CGRectGetMidX(self.frame), self.frame.size.height - 70)
addChild(scoreLabel)
let birdTexture = SKTexture(imageNamed: "flappy1.png")
let birdTexture2 = SKTexture(imageNamed: "flappy2.png")
let animation = SKAction.animateWithTextures([birdTexture,birdTexture2], timePerFrame: 0.1)
let makeBirdFlap = SKAction.repeatActionForever(animation)
bird = SKSpriteNode(texture: birdTexture)
bird.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
bird.runAction(makeBirdFlap)
bird.physicsBody = SKPhysicsBody(circleOfRadius: birdTexture.size().height/2)
bird.physicsBody!.dynamic = true
bird.physicsBody!.categoryBitMask = ColliderType.Bird.rawValue
bird.physicsBody?.contactTestBitMask = ColliderType.Object.rawValue
bird.physicsBody?.collisionBitMask = ColliderType.Object.rawValue
self.addChild(bird)
var ground = SKNode()
ground.position = CGPointMake(0, 0)
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.size.width, 1))
ground.physicsBody!.dynamic = false
ground.physicsBody!.categoryBitMask = ColliderType.Object.rawValue
ground.physicsBody?.contactTestBitMask = ColliderType.Object.rawValue
ground.physicsBody?.collisionBitMask = ColliderType.Object.rawValue
self.addChild(ground)
_ = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("makePipes"), userInfo: nil, repeats: true)
}
func makePipes () {
let gapHeight = bird.size.height * 4
let movementAmount = arc4random() % UInt32(self.frame.size.height / 2)
let pipeOffset = CGFloat(movementAmount) - self.frame.size.height / 4
let movePipes = SKAction.moveByX(-self.frame.size.width * 2, y: 0, duration: NSTimeInterval (self.frame.size.width / 100))
let removePipes = SKAction.removeFromParent()
let moveAndRemovePipes = SKAction.sequence([movePipes,removePipes])
let pipeTexture1 = SKTexture(imageNamed: "pipe1.png")
let pipe1 = SKSpriteNode(texture: pipeTexture1)
pipe1.position = CGPoint(x: CGRectGetMidX(self.frame) + self.frame.size.width, y: CGRectGetMidY(self.frame) + pipeTexture1.size().height/2 + gapHeight/2 + pipeOffset)
pipe1.runAction(moveAndRemovePipes)
pipe1.physicsBody = SKPhysicsBody(rectangleOfSize: pipeTexture1.size())
pipe1.physicsBody?.dynamic = false
pipe1.physicsBody!.categoryBitMask = ColliderType.Object.rawValue
pipe1.physicsBody?.contactTestBitMask = ColliderType.Object.rawValue
pipe1.physicsBody?.collisionBitMask = ColliderType.Object.rawValue
movingObjects.addChild(pipe1)
let pipeTexture2 = SKTexture(imageNamed: "pipe2.png")
let pipe2 = SKSpriteNode(texture: pipeTexture2)
pipe2.position = CGPoint(x: CGRectGetMidX(self.frame) + self.frame.size.width, y: CGRectGetMidY(self.frame) - pipeTexture2.size().height/2 - gapHeight/2 + pipeOffset)
pipe2.runAction(moveAndRemovePipes)
pipe2.physicsBody = SKPhysicsBody(rectangleOfSize: pipeTexture2.size())
pipe2.physicsBody!.dynamic = false
pipe2.physicsBody!.categoryBitMask = ColliderType.Object.rawValue
pipe2.physicsBody!.contactTestBitMask = ColliderType.Object.rawValue
pipe2.physicsBody!.collisionBitMask = ColliderType.Object.rawValue
movingObjects.addChild(pipe2)
var gap = SKNode()
gap.position = CGPoint(x: CGRectGetMidX(self.frame) + self.frame.size.width, y: CGRectGetMidY(self.frame) + pipeOffset)
gap.runAction(moveAndRemovePipes)
gap.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(pipe1.size.width, gapHeight))
gap.physicsBody?.dynamic = false
gap.physicsBody!.categoryBitMask = ColliderType.Gap.rawValue
gap.physicsBody!.contactTestBitMask = ColliderType.Bird.rawValue
gap.physicsBody!.collisionBitMask = ColliderType.Gap.rawValue
movingObjects.addChild(gap)
}
func didBeginContact(contact: SKPhysicsContact) {
if contact.bodyA.categoryBitMask == ColliderType.Gap.rawValue || contact.bodyB.categoryBitMask == ColliderType.Gap.rawValue {
score++
scoreLabel.text = String(score)
} else {
if gameOver == false {
gameOver = true
self.speed = 0
gameOverLabel.fontName = "Helvetica"
gameOverLabel.fontSize = 30
gameOverLabel.text = "Game is over. Tap to play again."
gameOverLabel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
labelContainer.addChild(gameOverLabel)
}
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if gameOver == false {
bird.physicsBody!.velocity = CGVectorMake(0, 0)
bird.physicsBody!.applyImpulse(CGVectorMake(0, 50))
}
else
{
score = 0
scoreLabel.text = "0"
bird.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
bird.physicsBody?.velocity = CGVectorMake(0, 0)
bird.physicsBody!.allowsRotation = false
movingObjects.removeAllChildren()
makeBg()
self.speed = 1
gameOver = false
labelContainer.removeAllChildren()
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.