Jump to content

Where is the error? (update function)


Recommended Posts

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

Link to comment
Share on other sites



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));    }});});
Link to comment
Share on other sites

Hello again guys :D 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: spriteAnimation

So 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 ^^

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?
Uhm into the documentation I didn't see anything about sprite angle, am I wrong?
this is what I'm trying to reach:
Link to comment
Share on other sites

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.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...