Twiggy

Did I miss something?[Solved]

Recommended Posts

game.module(
    'game.main'
)
.body(function() {
    game.addAsset('square.png');

game.createScene('Main', {
    backgroundColor: 'blue',
    init: function() {
                //add Physics
        this.world = new game.Physics();
        this.world.gravity.y = 2000;
        
        //boundary physics body(floor)
        var floor = new game.Body();
        var shape = new game.Rectangle();
        //shape of physics body
        shape.width = game.width;
        shape.height = 60;
        //position for floor physics body
        floor.position.x = game.width / 2;
        floor.position.y = game.height - 30;
        //collision
        floor.collisionGroup = 1;
        floor.static = true;
        //add created shape to body
        floor.addShape(shape);
        //add body to world
        floor.addTo(this.world);
        
        //roof / top collision
        var roof = new game.Body();
        roof.position.x = game.width / 2;
        roof.position.y = 30;
        roof.collisionGroup = 1;
        roof.static = true;
        roof.addShape(shape);
        roof.addTo(this.world);
        
        //Left / Right Border collision
        var borderL = new game.Body();
        var borderShape = new game.Rectangle();
        
        borderShape.width = 60;
        borderShape.height = 900;
        borderL.position.x = 30;
        borderL.position.y = game.height / 2;
        borderL.collisionGroup = 2;
        borderL.static = true;
        borderL.addShape(borderShape);
        borderL.addTo(this.world);
        
        
        var borderR = new game.Body();
        borderR.position.x = game.width - 30;
        borderR.position.y = game.height / 2;
        borderR.collisionGroup = 2;
        borderR.static = true;
        borderR.addShape(borderShape);
        borderR.addTo(this.world);
        
        //containers
        this.obstacleLayer = new game.Container();
        this.playerLayer = new game.Container();
        this.collectableLayer = new game.Container();
        
        //add containers to the stage
        this.obstacleLayer.addTo(this.stage);
        this.playerLayer.addTo(this.stage);
        this.collectableLayer.addTo(this.stage);
        
        this.player = new game.Player();
        this.player.sprite.addTo(this.world);
        
    }
});

game.createClass('Player', {
    init: function() {
        this.sprite = new game.Sprite('square.png');
        this.sprite.anchorCenter();
        this.body = new game.Body();
        this.body.position.x = game.width / 2;
        this.body.position.y = game.height - 100;
        
        //possible collision groups
        this.body.collideAgainst = [1,2];
        this.body.velocityLimit.y = 1150;
        
        var shape = new game.Rectangle();
        shape.width = this.sprite.width;
        shape.height = this.sprite.height;
        
        this.body.addShape(shape);
        this.body.addTo(game.scene.world);
        
        this.body.collide = this.collide.bind(this);
    }
});

});

Hey Guys,

work finally slowed down enough to give me time to give Panda2 another shot but I need some help.

I receive this error, but am not sure what it means when I edit the Player class "this.body.collide = this .collide.bind(this);

Uncaught TypeError: Cannot read property 'indexOf' of undefined

when I try to comment that section out it gives me

Uncaught TypeError: Cannot read property 'indexOf' of undefined

 

what am I missing?

 

 

Also if I made an error in how I posted let me know so I dont in the future.

Share this post


Link to post
Share on other sites

Hi @Twiggy

I did take a quick look at your code and spotted one issue.

this.body.collide = this.collide.bind(this);

You are binding your body collide function to new collide function in your Player class, but you have not created that function.

You should create it, like this:

game.createClass('Player', {
    init: function() {
        this.sprite = new game.Sprite('square.png');
        this.sprite.anchorCenter();
        this.body = new game.Body();
        this.body.position.x = game.width / 2;
        this.body.position.y = game.height - 100;
        
        //possible collision groups
        this.body.collideAgainst = [1,2];
        this.body.velocityLimit.y = 1150;
        
        var shape = new game.Rectangle();
        shape.width = this.sprite.width;
        shape.height = this.sprite.height;
        
        this.body.addShape(shape);
        this.body.addTo(game.scene.world);
        
        this.body.collide = this.collide.bind(this);
    },

    collide: function(body) {
        // Body collides with another body
    }
});

 

Share this post


Link to post
Share on other sites

Hello @enpu,

I still received the :Uncaught TypeError: Cannot read property "indexOf" of undefined. Im going to try rewriting that class entry altogether. 

 

Thank you for the speedy reply :D

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

  • Recently Browsing   0 members

    No registered users viewing this page.