in Education by
Can anyone help me, Im trying to make a simple app where you touch the screen and 4 images are randomly displayed. Problem is, when i tap the screen i don't get any image also I have no errors. Help! Here's my code: import SpriteKit class GameScene: SKScene { var num1 = SKSpriteNode(imageNamed: "num1") var num2 = SKSpriteNode(imageNamed: "num2") var num3 = SKSpriteNode(imageNamed: "num3") var num4 = SKSpriteNode(imageNamed: "num4") override func didMoveToView(view: SKView) { self.physicsWorld.gravity = CGVectorMake(0, -9.8) num1.size = CGSize(width: 150, height: 150) num2.size = CGSize(width: 150, height: 150) num3.size = CGSize(width: 150, height: 150) num4.size = CGSize(width: 150, height: 150) let sceneBody = SKPhysicsBody(edgeLoopFromRect: self.frame) sceneBody.friction = 0 self.physicsBody = sceneBody } override func touchesBegan(touches: Set, withEvent event: UIEvent?) { for touch: AnyObject in touches { _ = touch.locationInNode(self) let random = arc4random_uniform(4) switch random { case 0: self.addChild(num1) print("number 0") case 1: self.addChild(num2) print("number 1") case 2: self.addChild(num3) print("number 2") default: self.addChild(num4) print("default") } } } } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
you are not setting the position where each sprite should appear Before each self.addChild(...) you should exec something like this numX.position = location and rearrange your touchesBegan method like this override func touchesBegan(touches: Set, withEvent event: UIEvent?) { var location for touch in touches { location = touch.locationInNode(self) } let random = arc4random_uniform(4) switch random { case 0: num1.position = location self.addChild(num1) print("number 0") case 1: num2.position = location self.addChild(num2) print("number 1") case 2: num3.position = location self.addChild(num3) print("number 2") default: num4.position = location self.addChild(num4) print("default") } }

Related questions

0 votes
    Can anyone help me, Im trying to make a simple app where you touch the screen and 4 images are ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I am working on a code pen and am having an issue where initially my main element would be exactly ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I have a UITableView inside a UITableViewCell (Scrolling is disabled for the nested UITableView) Everything works ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I have a UITableView inside a UITableViewCell (Scrolling is disabled for the nested UITableView) Everything works ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 26, 2022 in Education by JackTerrance
0 votes
    I have a UITableView inside a UITableViewCell (Scrolling is disabled for the nested UITableView) Everything works ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    I make an alarm clock app, and I want when my device is locked and alarm start calling see a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I am using Realm with Swift 3 in my iOS app. I have the following code //Find all records for the day func ... { let predicate = NSPredicate(format: "date >= %@ and date...
asked May 8, 2022 in Education by JackTerrance
0 votes
    I am writing my first iOS application (iPhone only) with Swift. The main application view should allow user ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 29, 2022 in Education by JackTerrance
0 votes
    Recently implemented trailingSwipeActionsConfigurationForRowAt , where after swiping from right to left showing two options ... , JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    Is it possible to get access to dismiss() in an SKScene class? Dismiss is a method available from ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I've inherited a codebase that implements a half-baked implementation of custom intents and shortcuts. One ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I'm making a little game and I'am having trouble updating an UILabel I created to display the current ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I have a view with a text field in it, but the text field is at the bottom. When I click the keyboard, it pops up ... to push the rest of the view up when a keyboard comes up?...
asked Mar 9, 2021 by JackTerrance
0 votes
    This question already has answers here: jQuery in Greasemonkey 1.0 conflicts with websites using jQuery (3 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    When you search for 'Doctor' on a search website, the results for Doctor images might mostly come up as 'male'. ... d) Lack of access Select the correct answer from above options...
asked Nov 13, 2021 in Education by JackTerrance
...