ghclark2

what context to pass for weapon.fire function

Recommended Posts

Hi,

I am really confused why I am getting errors with this code from the weapon plugin:
 

  create: function() {
    //load current level
    this.loadLevel();

    this.weapon = this.game.add.weapon(-1, 'explode');
    this.weapon.fireFrom.setTo(300,300, 1, 1);

    this.weapon.bulletKillType = Phaser.Weapon.KILL_CAMERA_BOUNDS;
    this.weapon.bulletGravity.y = -1000;

    this.input.keyboard.addKey(Phaser.KeyCode.SPACEBAR)
        .onDown.add(this.weapon.fire, this);

The error is:

phaser.js:100951 Uncaught TypeError: Cannot read property 'width' of undefined
    at Object.Phaser.Weapon.fire (phaser.js:100951)

It seems to be an issue with the context, but I have tried this.weapon, this.state, this.game all to no avail. Annoyingly if I run ZPlat.GameState.weapon.fire() from the console it shoots fine.

If anyone can see what I'm doing wrong I'd be very grateful.

Thanks, Gordon

Share this post


Link to post
Share on other sites
add(this.weapon.fire, this.weapon)

should be the right context, but probably that's passing the signal's arguments into fire, where they don't match. So try it just as

add(function () { this.weapon.fire(); }, this)

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.