I apologise but I am relatively new to coding and venturing into typing up my own customisations of a flappy bird game. I have made two customisable buttons, for the bird and for the background. The bird button is fully functional but when the game runs and if I have clicked the background button on the main menu it has changed in the game but the button itself does not change on the main menu? Any help would be much appreciated, thank you!
import Foundation
class GameManager {
static let instance = GameManager();
private init() {}
var birdIndex = Int(0);
var birds = ["Blue", "Green", "Red"];
func incrementIndex() {
birdIndex += 1;
if birdIndex == birds.count {
birdIndex = 0
}
}
func getBird() -> String {
return birds[birdIndex];
}
var backIndex = Int(0);
var backs = ["Day", "Night", "Preme"];
func otherincrementIndex() {
backIndex += 1;
if backIndex == backs.count {
backIndex = 0;
}
}
func getBack() -> String {
return backs[backIndex];
}
func setHighscore(highscore: Int) {
UserDefaults.standard.set(highscore, forKey: "Highscore");
}
func getHighscore() -> Int {
return UserDefaults.standard.integer(forKey: "Highscore");
}
}
The customisable bird button does work but the background one doesn't. Is this problem going to be caused by this section or the Gameplay scene? Thank you again in advance.
Related
I've got a complex application where I am using NSSplitView to create various sidebars which can be opened/shut with gravity (ie, drag the splitter bar close enough to the edge and the view closes completely) the same way XCode does it in it's UI.
Utilizing splitView(_:constrainSplitPosition:ofSubviewAt:) works great when the nested view being hidden does not contain a NSTabView / NSTabViewControllerView however if it does the window refuses to close completely leaving the tabView visible.
class ViewController: NSViewController, NSSplitViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
splitView.delegate = self
}
#IBOutlet var splitView: NSSplitView!
#IBOutlet var tabView: NSTabView!
let gravityTolerance: CGFloat = 180.0
func splitView(
_ splitView: NSSplitView,
constrainSplitPosition proposedPosition: CGFloat,
ofSubviewAt dividerIndex: Int
) -> CGFloat {
print("proposed splitter width: \(dividerIndex) => \(proposedPosition)")
var retVal = proposedPosition
if dividerIndex == 0 {
if proposedPosition <= gravityTolerance {
// tabView.isHidden = true
retVal = 0.0
} else {
// tabView.isHidden = false
}
}
return retVal
}
}
Setting the tab view as "isHidden" makes no difference and I'm pretty sure that if I hand code it all it will work fine. But is there some simple fix ( constraints perhaps ) that I'm missing?
From Willeke's comment... works like a charm. Seems there's lots of Apple-specific support for this feature that I didn't know about: ( obviously one could get a lot fancier than this )
func splitView(
_ splitView: NSSplitView,
canCollapseSubview subview: NSView
) -> Bool
{
return true
}
I am creating a game and I am trying to keep a record of all enemy's killed but my SKLabel node is not updating. Here's how I'm implementing it
class GameScene: SKScene, SKPhysicsContactDelegate {
var Enemy1KillCounter:Int = 0
var Enemy1KillCounterLabel = SKLabelNode ()
override func didMoveToView(view: SKView) {
createEnemyKilledLabel()
}
func createEnemyKilledLabel() {
Enemy1KillCounterLabel.text = "\(Enemy1KillCounter)"
Enemy1KillCounterLabel.fontSize = 65
Enemy1KillCounterLabel.fontColor = SKColor .blackColor()
Enemy1KillCounterLabel.position = CGPointMake(400, 400)
self.addChild(Enemy1KillCounterLabel)
}
func updateEnemy1KillCounter() {
Enemy1KillCounter = Enemy1KillCounter + 1
print(Enemy1KillCounter)
}
// I use the next method because i call this method in my enemy class
when the enemy is "killed"
func Enemy1DieG () {
updateEnemy1KillCounter()
}
Does anybody know why my label is not being updated?
When you update Enemy1KillCounter, you also need to update the Enemy1KillCounterLabel.text with the new value. Besides, I don't see where your createEnemyKilledLabel() is called. Make sure it is called somewhere.
A side note - variable names typically start with lowercase, like enemy1KillCounterLabel. Following the standards makes the code easier to read by others...
Update your label text after updating your Enemy1KillCounter variable.
func updateEnemy1KillCounter() {
Enemy1KillCounter = Enemy1KillCounter + 1
Enemy1KillCounterLabel.text = "\(Enemy1KillCounter)"
print(Enemy1KillCounter)
}
I am working on a small SpriteKit game.
I have a "Tips" section on the Home Screen that I want to pulse in and out, each time displaying different Tips.
I have a method that works, which I wrote myself, but it's messy and I'm sure there's an better way it could be done. I was hoping someone could show me a way that maybe I missed (or went a long way around doing).
This is how I currently do it:
func createTipsLabels(){
//create SKLabelNodes
//add properties to Labels
//tip1Label... etc
//tip2Label... etc
//tip3Label... etc
//now animate (or pulse) in tips label, one at a time...
let tSeq = SKAction.sequence([
SKAction.runBlock(self.fadeTip1In),
SKAction.waitForDuration(5),
SKAction.runBlock(self.fadeTip1Out),
SKAction.waitForDuration(2),
SKAction.runBlock(self.fadeTip2In),
SKAction.waitForDuration(5),
SKAction.runBlock(self.fadeTip2Out),
SKAction.waitForDuration(2),
SKAction.runBlock(self.fadeTip3In),
SKAction.waitForDuration(5),
SKAction.runBlock(self.fadeTip3Out),
SKAction.waitForDuration(2),
])
runAction(SKAction.repeatActionForever(tSeq)) //...the repeat forever
}
//put in separate methods to allow to be called in runBlocks above
func fadeTip1In() { tip1Label.alpha = 0; tip1Label.runAction(SKAction.fadeInWithDuration(1)) ; print("1") }
func fadeTip1Out(){ tip1Label.alpha = 1; tip1Label.runAction(SKAction.fadeOutWithDuration(1)); print("2") }
func fadeTip2In() { tip2Label.alpha = 0; tip2Label.runAction(SKAction.fadeInWithDuration(1)) ; print("3") }
func fadeTip2Out(){ tip2Label.alpha = 1; tip2Label.runAction(SKAction.fadeOutWithDuration(1)); print("4") }
func fadeTip3In() { tip3Label.alpha = 0; tip3Label.runAction(SKAction.fadeInWithDuration(1)) ; print("5") }
func fadeTip3Out(){ tip3Label.alpha = 1; tip3Label.runAction(SKAction.fadeOutWithDuration(1)); print("6") }
How can I optimise this?
There is no need to create multiple labels nor do multiple actions, just create an array of what you want to do, and iterate through it.
func createTipsLabels()
{
let tips = ["1","2","3","4","5"];
var tipCounter = 0
{
didSet
{
if (tipCounter >= tips.count)
{
tipCounter = 0;
}
}
}
tipLabel.alpha = 0;
let tSeq = SKAction.sequence([
SKAction.runBlock({[unowned self] in self.tipLabel.text = tips[tipCounter]; print(tips[tipCounter]); tipCounter+=1;}),
SKAction.fadeInWithDuration(1),
SKAction.waitForDuration(5),
SKAction.fadeOutWithDuration(1),
SKAction.waitForDuration(2)
])
tipLabel.runAction(SKAction.repeatActionForever(tSeq)) //...the repeat forever
}
In my class ViewController: NSViewController
I have the following code:
#IBAction override func mouseDown(theEvent: NSEvent) {
self.myLabel.textColor = NSColor.redColor()
//either of these work to set the labels text value
self.myLabel.objectValue = "Hello World"
self.myLabel.stringValue = "This is a test"
switch(self) {
case self.myLabel:
//change text of myLabel
break;
case self.myLabel1:
//change text of myLabel1
break;
case self.myLabel2:
//change text of myLabel2
break;
}
}
This works to change the text and color of a Label Control called myLabel, but I have 3 Label controls on the View how do I change the color on the one that sends the mouse down event? The only way I can think to do it is with the switch statement as in the code above. But I think there is a better way by some how using the sender of the event?
I'm new to OS X and Mac development and come from the .NET C# world so thanks for helping this Mac noob! Using the latest Swift and Xcode.
I did figure out how to determine if one of my NSTextField controls was clicked on in the mouseDown event of the view:
#IBAction override func mouseDown(theEvent: NSEvent) {
var event_location: NSPoint!
event_location = theEvent.locationInWindow
self.mouseDownEvent = theEvent
var cntrl_id = NSTextField()
var cntrl_frame = NSRect()
var cntrl_name = String()
var cntrl_value = String()
var hit = Bool()
for view in self.view.subviews as [NSView] {
if let ct = view as? NSTextField {
cntrl_name = ct.identifier!
cntrl_id = ct
cntrl_frame = ct.frame
cntrl_value = ct.stringValue
hit = cntrl_frame.contains(event_location)
if hit {
controlToMove = cntrl_id
break
}
}
}
}
There are probably ways to make this more efficient possibly such as keeping a dictionary of all the NSTextField controls on the view, and then
check the dictionary for if I have a "hit" on one of the controls.
I'm attempting to add support for Voice Over accessibility in a puzzle game which has a fixed board. However, I'm having trouble getting UIAccessibilityElements to show up.
Right now I'm overriding accessibilityElementAtIndex, accessibilityElementCount and indexOfAccessibilityElement in my SKScene.
They are returning an array of accessible elements as such:
func loadAccessibleElements()
{
self.isAccessibilityElement = false
let pieces = getAllPieces()
accessibleElements.removeAll(keepCapacity: false)
for piece in pieces
{
let element = UIAccessibilityElement(accessibilityContainer: self.usableView!)
element.accessibilityFrame = piece.getAccessibilityFrame()
element.accessibilityLabel = piece.getText()
element.accessibilityTraits = UIAccessibilityTraitButton
accessibleElements.append(element)
}
}
Where piece is a subclass of SKSpriteNode and getAccessibilityFrame is defined:
func getAccessibilityFrame() -> CGRect
{
return parentView!.convertRect(frame, toView: nil)
}
Right now one (wrongly sized) accessibility element seems to appear on the screen in the wrong place.
Could someone point me in the right direction?
Many thanks
EDIT:
I've tried a hack-ish work around by placing a UIView over the SKView with UIButton elements in the same location as the SKSpriteNodes. However, accessibility still doesn't want to work. The view is loaded as such:
func loadAccessibilityView()
{
view.isAccessibilityElement = false
view.accessibilityElementsHidden = false
skView.accessibilityElementsHidden = false
let accessibleSubview = UIView(frame: view.frame)
accessibleSubview.userInteractionEnabled = true
accessibleSubview.isAccessibilityElement = false
view.addSubview(accessibleSubview)
view.bringSubviewToFront(accessibleSubview)
let pieces = (skView.scene! as! GameScene).getAllPieces()
for piece in pieces
{
let pieceButton = UIButton(frame: piece.getAccessibilityFrame())
pieceButton.isAccessibilityElement = true
pieceButton.accessibilityElementsHidden = false
pieceButton.accessibilityTraits = UIAccessibilityTraitButton
pieceButton.setTitle(piece.getText(), forState: UIControlState.Normal)
pieceButton.setBackgroundImage(UIImage(named: "blue-button"), forState: UIControlState.Normal)
pieceButton.alpha = 0.2
pieceButton.accessibilityLabel = piece.getText()
pieceButton.accessibilityFrame = pieceButton.frame
pieceButton.addTarget(self, action: Selector("didTap:"), forControlEvents: UIControlEvents.TouchUpInside)
accessibleSubview.addSubview(pieceButton)
}
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, nil)
}
The buttons are placed correctly, however accessibility just isn't working at all. Something seems to be preventing it from working.
I've searched in vain for a description of how to implement VoiceOver in Swift using SpriteKit, so I finally figured out how to do it. Here's some working code that converts a SKNode to an accessible pushbutton when added to a SKScene class:
// Add the following code to a scene where you want to make the SKNode variable named “leave” an accessible button
// leave must already be initialized and added as a child of the scene, or a child of other SKNodes in the scene
// screenHeight must already be defined as the height of the device screen, in points
// Accessibility
private var accessibleElements: [UIAccessibilityElement] = []
private func nodeToDevicePointsFrame(node: SKNode) -> CGRect {
// first convert from frame in SKNode to frame in SKScene's coordinates
var sceneFrame = node.frame
sceneFrame.origin = node.scene!.convertPoint(node.frame.origin, fromNode: node.parent!)
// convert frame from SKScene coordinates to device points
// sprite kit scene origin is in lower left, accessibility device screen origin is at upper left
// assumes scene is initialized using SKSceneScaleMode.Fill using dimensions same as device points
var deviceFrame = sceneFrame
deviceFrame.origin.y = CGFloat(screenHeight-1) - (sceneFrame.origin.y + sceneFrame.size.height)
return deviceFrame
}
private func initAccessibility() {
if accessibleElements.count == 0 {
let accessibleLeave = UIAccessibilityElement(accessibilityContainer: self.view!)
accessibleLeave.accessibilityFrame = nodeToDevicePointsFrame(leave)
accessibleLeave.accessibilityTraits = UIAccessibilityTraitButton
accessibleLeave.accessibilityLabel = “leave” // the accessible name of the button
accessibleElements.append(accessibleLeave)
}
}
override func didMoveToView(view: SKView) {
self.isAccessibilityElement = false
leave.isAccessibilityElement = true
}
override func willMoveFromView(view: SKView) {
accessibleElements = []
}
override func accessibilityElementCount() -> Int {
initAccessibility()
return accessibleElements.count
}
override func accessibilityElementAtIndex(index: Int) -> AnyObject? {
initAccessibility()
if (index < accessibleElements.count) {
return accessibleElements[index] as AnyObject
} else {
return nil
}
}
override func indexOfAccessibilityElement(element: AnyObject) -> Int {
initAccessibility()
return accessibleElements.indexOf(element as! UIAccessibilityElement)!
}
Accessibility frames are defined in the fixed physical screen coordinates, not UIView coordinates, and transforming between them is kind of tricky.
The device origin is the lower left of the screen, with X up, when the device is in landscape right mode.
It's a pain converting, I've no idea why Apple did it that way.