Phempt Posted October 18, 2014 Share Posted October 18, 2014 Hello guys, I'm trying to get this code working:game.module( 'game.main').require( 'engine.core').body(function() { game.addAsset('box.png'); eNemies = game.Class.extend({ init: function(vel, px){ this.containerEnemies = new game.Container(); this.containerEnemies.addTo(game.scene.stage); this.sprite = new game.Sprite('box.png',px,500,{anchor: { x: 0.5, y: 0.5 }}); this.sprite.interactive=true; this.sprite.addTo(this.containerEnemies); this.sprite.touchstart = this.sprite.click = function(){ this.remove(); } }, update: function(){ if(game.system.paused != true){ speedRandom = Math.floor(Math.random() * 280) + 100; this.sprite.position.y -= speedRandom * game.system.delta; } }, remove: function(){ game.scene.removeObject(this); } });game.createScene('Main', { backgroundColor: 0x44cce2, init: function() { eNemiesArray = []; i = 0; createEnemies = function(){ posXrandom = Math.floor(Math.random() * 500) + 1; this.eNemiesArray[i]=new eNemies(120,posXrandom); i++; setTimeout("createEnemies();", 250); } createEnemies(); }, });Enemies creation is working, enemies onTouch event also, but the update function, to move every enemies seems to not work, can you help me to understand what's wrong? Thank you Quote Link to comment Share on other sites More sharing options...
enpu Posted October 18, 2014 Share Posted October 18, 2014 Hi, I have made some changes to your code, with comments. Hope this helps you:game.module( 'game.main')// No need to require engine.core from game.main.body(function() { game.addAsset('box.png');// Use createClass to place your classes inside game namespacegame.createClass('eNemies', { init: function(vel, px){ this.sprite = new game.Sprite('box.png', px, 500, { anchor: { x: 0.5, y: 0.5 } }); this.sprite.interactive = true; this.sprite.touchstart = this.sprite.click = this.remove.bind(this); this.sprite.addTo(game.scene.containerEnemies); // Use addObject, to get update function called every frame game.scene.addObject(this); }, remove: function() { this.sprite.remove(); game.scene.removeObject(this); }, update: function() { // Is this what you want? Now your speed is changing every frame? var speedRandom = Math.floor(Math.random() * 280) + 100; this.sprite.position.y -= speedRandom * game.system.delta; } });game.createScene('Main', { backgroundColor: 0x44cce2, eNemiesArray: [], init: function() { // If you want to put your enemies into own container, create it here this.containerEnemies = new game.Container(); this.containerEnemies.addTo(this.stage); this.createEnemies(); }, createEnemies: function() { var posXrandom = Math.floor(Math.random() * 500) + 1; this.eNemiesArray.push(new game.eNemies(120, posXrandom)); // Instead of setTimeout, use addTimer, because that is controlled by game engine this.addTimer(250, this.createEnemies.bind(this)); }});}); Phempt 1 Quote Link to comment Share on other sites More sharing options...
Phempt Posted October 18, 2014 Author Share Posted October 18, 2014 @enpu, THANK YOU. you told me a lot of useful tips! Quote Link to comment Share on other sites More sharing options...
Phempt Posted October 18, 2014 Author Share Posted October 18, 2014 Hello again guys Can you help me in order to understand why this code:this.spriteAnimation = new game.Animation("test_01.png", "test_02.png", "test_03.png", "test_04.png");this.spriteAnimation.position.set(this.position.x, this.position.y);this.spriteAnimation.anchor.set(0.5, 0.5);this.spriteAnimation.scale.set(0.8, 0.8);this.spriteAnimation.animationSpeed = 0.1;this.spriteAnimation.loop = false;this.spriteAnimation.play();game.scene.stage.addChild(this.spriteAnimation);spriteAnimation.onComplete(function() { this.spriteAnimation.remove(); game.scene.removeObject(this); });This code works well, an animation will spawn on "this.position.x, this.position.y", but the onComplete function give me this browser error:ReferenceError: Can't find variable: spriteAnimationSo I tried to edit the onComplete function:this.spriteAnimation.onComplete(function() { game.scene.stage.removeChild(this); });The error changed to:TypeError: null is not a function (evaluating 'this.spriteAnimation.onComplete')Thank you in advance ^^ Quote Link to comment Share on other sites More sharing options...
enpu Posted October 18, 2014 Share Posted October 18, 2014 I'll try to explain it to you with this example:game.createClass('MySprite', { init: function() { this.anim = new game.Animation('anim1.png', 'anim2.png'); this.anim.play(); this.anim.onComplete(function() { // You are now inside new function // so "this" is not referring to your class like above this.anim.remove(); }); // Instead, bind onComplete into new function inside your class this.anim.onComplete(this.removeSprite.bind(this)); }, removeSprite: function() { // Now this works! this.anim.remove(); }});Hope this helps you Quote Link to comment Share on other sites More sharing options...
Phempt Posted October 18, 2014 Author Share Posted October 18, 2014 Ok, so I need to create a class to manage the animation! Thank you!Is there a way with panda to detect the swipe touch? for example as Fruit Ninja, is there a way to detect this ? Again, THANK YOU ^^ Quote Link to comment Share on other sites More sharing options...
Phempt Posted October 19, 2014 Author Share Posted October 19, 2014 Well, this seems to work:var startPoint; var endPoint; var prova = new game.Sprite('brush.png'); prova.anchor.set(0.0,0.0); prova.position.set(0, 0); prova.addTo(game.scene.backgroundContainer); prova.interactive=true; prova.touchstart = function (touchData) { console.log('asdasd'); startPoint = touchData.getLocalPosition(this.parent); } prova.touchend = function (touchData) { endPoint = touchData.getLocalPosition(this.parent); var xDistanceChecker = Math.abs(startPoint.x - endPoint.x); var yDistanceChecker = Math.abs(startPoint.y - endPoint.y); if (xDistanceChecker > 40 || yDistanceChecker > 40) { if (xDistanceChecker > yDistanceChecker) { if (endPoint.x < startPoint.x) console.log("swap left"); else console.log("swap right"); } else { if (endPoint.y < startPoint.y) console.log("swap up"); else console.log("swap down"); } } }is there a better way?Is there a way to add a "platform" between startPoint and endPoint? Quote Link to comment Share on other sites More sharing options...
Phempt Posted October 19, 2014 Author Share Posted October 19, 2014 Maybe I'm wrong but I can create the sprite with x: startPoint.x and y: startPoing.y and than add an angle usingi:angleInDegrees = Math.atan(yDistanceChecker / xDistanceChecker) * 180 / Math.PI;angleInDegrees = parseInt(angleInDegrees);This method could work, I think.. but the created platform is not long as the distance between startPoint and endPoint. Any suggestion? Edit:Uhm into the documentation I didn't see anything about sprite angle, am I wrong? this is what I'm trying to reach: Quote Link to comment Share on other sites More sharing options...
enpu Posted October 19, 2014 Share Posted October 19, 2014 You want to rotate sprite? https://gist.github.com/ekelokorpi/9720045 Quote Link to comment Share on other sites More sharing options...
Phempt Posted October 19, 2014 Author Share Posted October 19, 2014 I've the angle in degrees, how to use it with rotation? Quote Link to comment Share on other sites More sharing options...
Phempt Posted October 19, 2014 Author Share Posted October 19, 2014 Ok, I used the radians one Thank you ^^ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.