Sign in to follow this  
lars

Collision and World

Recommended Posts

Hello

Im struggling with some understanding of useing the World obejct  for collision.

 

The problem is:

 

I having a Pumking class where im trying to create body for collision detection.

 

   game.createClass('Pumpkins', {        init: function() {            console.log("Pumkings");            this.sprite = new game.Sprite('pumkings');            this.sprite.y = game.system.height / 2 - this.sprite.height / 2 + 150            this.sprite.interactive = true;            this.sprite.addTo(game.scene.stage);            /*              this.world = new game.World();             */            this.body = new game.Body({                position: {                    x: 100,                    y: 100                },                velocityLimit: {                    x: 150,                    y: 150                },                velocity: {                    x: -100,                    y: -50                },                collisionGroup: 1,                collideAgainst: 0,                mass: 0            });            this.body.collide = this.collide.bind(this);            this.body.addShape(new game.Rectangle(60, 60));            game.scene.addObject(this);            this.world.addBody(this.body);// this is where my error is thrown            game.scene.stage.addChild(this); },        collide: function(opponent) {          console.log("Collision!");           return true;        },        update: function() {   this.sprite.position.y += 50 * game.system.delta;        } });

then in my Main scene, which for now is in the same file, I create my World object:

 

    game.createScene('Main', {        backgroundColor: 0x630909,        init: function() {            console.log("Scene");    this.world = new game.World(0, 0); // my world    this.initGame();        },        initGame: function() {       player = new game.Alien(100, 100);//just another class       pumkings = new game.Pumpkins();             }    });

Im getting the following error:

 

Cannot read property 'addBody' of undefined. 

 

So ... maybee my World is´nt global or ...?

 

I posting my entire code below:

 

game.module(    'game.main').require(    'engine.core',    'engine.keyboard',    'engine.tween',    'engine.physics').body(function() {    var player;    var Ninja;    var myPumkings;    game.addAsset('ninja_1.png', 'ninja1');    game.addAsset('ninja_2.png', 'ninja2');    game.addAsset('ninja_3.png', 'ninja3');    game.addAsset('pumkings.png', 'pumkings');    //game.addAsset('ninja.json');    game.addAudio('swipe.mp3', 'swipe');    game.createClass('Alien', {        name: 'Hans',        health: 10,        init: function(myX, myY) {            console.log("Alien");            Ninja = new game.Animation('media/ninja_1.png', 'media/ninja_2.png', 'media/ninja_3.png', 'media/ninja_4.png');            Ninja.animationSpeed = 0.1;            Ninja.position.set(game.system.width / 2 - Ninja.width / 2, game.system.height - Ninja.height);            //Ninja.anchor.set(0,1);            Ninja.addTo(game.scene.stage);        }, //init        mousedown: function() {            console.log("allo");            game.audio.playSound("swipe", false);        }    });    game.createClass('Pumpkins', {        init: function() {            console.log("Pumkings");            this.sprite = new game.Sprite('pumkings');            this.sprite.y = game.system.height / 2 - this.sprite.height / 2 + 150            this.sprite.interactive = true;            this.sprite.addTo(game.scene.stage);                         this.world = new game.World();                     this.body = new game.Body({                position: {                    x: 100,                    y: 100                },                velocityLimit: {                    x: 150,                    y: 150                },                velocity: {                    x: -100,                    y: -50                },                collisionGroup: 1,                collideAgainst: 0,                mass: 0            });            this.body.collide = this.collide.bind(this);            this.body.addShape(new game.Rectangle(60, 60));            //add sprite to scene            game.scene.addObject(this);            //add body of this sprite to the world object         this.world.addBody(this.body);            /*        //add sprite to display container        game.scene.stage.addChild(this);                 */        },        collide: function(opponent) {            console.log("Collision!");            //opponent.remove();            return true;        },        update: function() {            //myPumkings.position.x = this.body.position.x;            //myPumkings.position.y = this.body.position.y;            //this.sprite.position.y += 50 * game.system.delta;        }    });    game.createScene('Main', {        backgroundColor: 0x630909,        init: function() {            console.log("Scene");            this.world = new game.World(0, 0);            this.initGame();            /*         var tweenPumkings = new game.Tween(myPumkings.position);        tweenPumkings.to({            x: (100)                   }, 1000);tweenPumkings.easing(game.Tween.Easing.Elastic.Out);   tweenPumkings.start();    */        },        initGame: function() {            player = new game.Alien(100, 100);            pumkings = new game.Pumpkins();                    },        keyup: function(key) {            if (key === 'RIGHT') {                Ninja.gotoAndStop(2);                game.audio.playSound("swipe", false);                goTweenAlien(+150);            }            if (key === 'LEFT') {                Ninja.gotoAndStop(1);                game.audio.playSound("swipe", false);                goTweenAlien(-150);            }            if (key === 'UP') {                Ninja.gotoAndStop(3);                game.audio.playSound("swipe", false);                jumpAlien();            }        } //end keyup    });    function goTweenAlien(move) {        var tweenRight = new game.Tween(Ninja.position);        tweenRight.to({            x: (game.system.width / 2 - Ninja.width / 2) + move        }, 300);        tweenRight.easing(game.Tween.Easing.Elastic.Out);        tweenRight.onComplete(function() {            tweenOver();        });        tweenRight.start();    }    function jumpAlien() {        var tweenUp = new game.Tween(Ninja.position);        tweenUp.to({            y: (0)        }, 300);        tweenUp.easing(game.Tween.Easing.Elastic.Out);        tweenUp.onComplete(function() {            tweenOver();        });        tweenUp.start();    }    function tweenOver() {        var tweenBack = new game.Tween(Ninja.position);        tweenBack.to({            x: (game.system.width / 2 - Ninja.width / 2),            y: game.system.height - Ninja.height        }, 50);        tweenBack.delay(100);        tweenBack.start();        tweenBack.onComplete(function() {            Ninja.gotoAndStop(0);        });    }    //tweenRight.chain(tweenBack);});

Hope someone can help me here ... 

 

 

 

 

Share this post


Link to post
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.

Guest
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.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.