質問するログイン新規登録

回答編集履歴

1

サンプルソースをObjectiveCで書いてしまったのでSwiftに修正

2015/02/15 14:20

投稿

jollyjoester
jollyjoester

スコア1585

answer CHANGED
@@ -3,34 +3,33 @@
3
3
  タッチされているかの検知はtouchesBeganとtouchedEndedでフラグの上げ下げすればよいかと。
4
4
 
5
5
  ```
6
- #import "SampleScene.h"
6
+ import SpriteKit
7
7
 
8
- @implementation SampleScene{
8
+ class GameScene: SKScene {
9
+
9
- BOOL isTouched;  //タッチされているかフラグ
10
+ var isTouched: Bool = false
10
- SKSpriteNode *someSprite; //動かしたいspriteをここに入れる
11
+ var someSprite: SKSpriteNode! //動かしたいspriteを入れる
11
- }
12
12
 
13
- // ごにょごにょ
13
+ //ごにょごにょ
14
14
 
15
- -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
15
+ override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
16
- {
17
- if (//タッチされたのが対象のUIButtonだったら) {
16
+ if (//されたのが対象のボタンだったら) {
18
- isTouched = YES;
17
+ isTouched = true
18
+ }
19
19
  }
20
+
21
+ override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
22
+ if (//押されたのが対象のボタンだったら) {
23
+ isTouched = false
20
- }
24
+ }
21
-
22
- -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
23
- {
24
- if (//タッチされたのが対象のUIButtonだったら) {
25
- isTouched = NO;
26
25
  }
26
+
27
+ override func update(currentTime: CFTimeInterval) {
28
+ if isTouched {
29
+ let x: CGFloat = someSprite.position.x
30
+ let y: CGFloat = someSprite.position.y
31
+ someSprite.position = CGPointMake(x+5, y+5)
27
- }
32
+ }
28
-
29
- -(void)update:(CFTimeInterval)currentTime {
30
- if (isTouched == YES) {
31
- float x = someSprite.position.x;
32
- float y = someSprite.position.y;
33
- someSprite.position = CGPointMake(x+5, y+5);
34
33
  }
35
34
  }
36
35
  ```