Browse Source

Added Touch control for breakout example

rsredsq 10 years ago
parent
commit
5bcd8ba402
2 changed files with 46 additions and 19 deletions
  1. 1 1
      Breakout/Resources/Components/Ball.js
  2. 45 18
      Breakout/Resources/Components/Paddle.js

+ 1 - 1
Breakout/Resources/Components/Ball.js

@@ -48,7 +48,7 @@ exports.component = function(self) {
             self.rigidBody.linearVelocity = [self.rigidBody.linearVelocity[0], MAX_SPEED];
 
         //check if a ball fell down
-        if (self.node.position2D[1] <= -4) {
+        if (self.node.position2D[1] <= -Atomic.graphics.height/2*Atomic.PIXEL_SIZE) {
             self.remove();
             self.sendEvent("CreateNewBall");
         }

+ 45 - 18
Breakout/Resources/Components/Paddle.js

@@ -20,6 +20,14 @@ exports.component = function(self) {
             self.createStartBall();
             self.started = false;
         });
+
+        self.subscribeToEvent("MultiGesture", function(event) {
+            if(event.numFingers >= 2) {
+                self.runBall();
+            }
+        });
+
+        self.node.position2D = [self.node.position2D[0], -Atomic.graphics.height/2.5*Atomic.PIXEL_SIZE];
     }
     self.createStartBall = function() {
         //create startBall prefab
@@ -30,38 +38,57 @@ exports.component = function(self) {
         self.startBallComponent = self.startBall.getJSComponent("Ball");
     }
 
+    self.runBall = function() {
+        //get rigidBody component of ours ball
+        var body = self.startBall.getComponent("RigidBody2D");
+        //let's run our ball!
+        body.applyForceToCenter([20 + Math.random(), 20 + Math.random()], true);
+        self.startBallComponent.started = true;
+        self.started = true;
+    }
+
     self.update = function(delta) {
         //if we haven't ball started
         if(!self.started) {
             //check mouse button click
             if(Atomic.input.getMouseButtonPress(Atomic.MOUSEB_LEFT) || Atomic.input.getKeyDown(Atomic.KEY_SPACE)) {
-                //get rigidBody component of ours ball
-                var body = self.startBall.getComponent("RigidBody2D");
-                //let's run our ball!
-                body.applyForceToCenter([20 + Math.random(), 20 + Math.random()], true);
-                self.startBallComponent.started = true;
-                self.started = true;
+                self.runBall();
             }
             //if we haven't started yet, move our ball with the paddle
             self.startBall.position2D = [self.node.position2D[0], self.node.position2D[1]+64*Atomic.PIXEL_SIZE];
         }
-        if (self.keyboard){
-            var pos = self.node.position2D;
-            if (Atomic.input.getKeyDown(Atomic.KEY_A) || Atomic.input.getKeyDown(Atomic.KEY_LEFT)) {
-                pos[0] -= PADDLE_SPEED;
-                self.node.position2D = pos;
-                // self.rigidBody.setLinearVelocity([-5, 0]);
-            } else if (Atomic.input.getKeyDown(Atomic.KEY_D) || Atomic.input.getKeyDown(Atomic.KEY_RIGHT)) {
-                pos[0] += PADDLE_SPEED;
-                self.node.position2D = pos;
-            }
-        } else {
+        if (Atomic.platform == "Android" || Atomic.platform == "iOS") {
+            //get touches
+            var numTouches = Atomic.input.getNumTouches();
+            //if we don't have touches, return
+            if (numTouches <= 0) return;
+            //get the last touch state
+            var touchState = Atomic.input.getTouch(numTouches-1);
             //get current mouse position, and project screen coordinates to the world coordinates
-            var pos = Atomic.renderer.getViewport(0).screenToWorldPoint(Atomic.input.getMousePosition()[0], 0, 0);
+            var pos = Atomic.renderer.getViewport(0).screenToWorldPoint(touchState.position[0], 0, 0);
             //set y value to -3
             pos[1] = -Atomic.graphics.height/2.5*Atomic.PIXEL_SIZE;
             // set paddle position to the calculated one
             self.node.position2D = pos;
+        } else {
+            if (self.keyboard) {
+                var pos = self.node.position2D;
+                if (Atomic.input.getKeyDown(Atomic.KEY_A) || Atomic.input.getKeyDown(Atomic.KEY_LEFT)) {
+                    pos[0] -= PADDLE_SPEED;
+                    self.node.position2D = pos;
+                    // self.rigidBody.setLinearVelocity([-5, 0]);
+                } else if (Atomic.input.getKeyDown(Atomic.KEY_D) || Atomic.input.getKeyDown(Atomic.KEY_RIGHT)) {
+                    pos[0] += PADDLE_SPEED;
+                    self.node.position2D = pos;
+                }
+            } else {
+                //get current mouse position, and project screen coordinates to the world coordinates
+                var pos = Atomic.renderer.getViewport(0).screenToWorldPoint(Atomic.input.getMousePosition()[0], 0, 0);
+                //set y value to -3
+                pos[1] = -Atomic.graphics.height/2.5*Atomic.PIXEL_SIZE;
+                // set paddle position to the calculated one
+                self.node.position2D = pos;
+            }
         }
         if(self.node.position2D[0] < -Atomic.graphics.width / 2*Atomic.PIXEL_SIZE) {
             self.node.position2D = [-Atomic.graphics.width / 2*Atomic.PIXEL_SIZE, -Atomic.graphics.height/2.5*Atomic.PIXEL_SIZE];