Jump to content

Change the controlled sprite


Jendrik
 Share

Recommended Posts

Hey,

how could I change the controlled sprite? In my game, I want that the player first controls the wolf and when schaaf reaches the end, the player will control the hund and not anymore the wolf. wolf will flee away to the left side.

in my actually code, the "reverse"-switch (boolean) doesn´t work. Means, when schaaf touches end, the boolean doesn´t switch to "true". But the console.log function is called.

 

My first idea was to switch the state. But that wasn´t working, too. "Unable to call undefined "state"..."blabla.

this.game.state.start("reverse");

This is my actually code without hindernis-stuff:

"use strict";window.wolf.state.play = {	preload: function(){		console.log("loading play state");	},		create: function(){		console.log("starting play state");				this.bg = mt.create("bg");		this.unten = mt.create("unten");		this.wolf = mt.create("wolf");		this.schaaf = mt.create("schaaf");		this.end = mt.create("end");		this.hund = mt.create("hund");				this._hindernis = this.add.group();		this.pos = [10];		var i = 0;		while(i != 10){			this.spawnHindernis(i);			i++;		}				this.reverse = false;		this.cursors = this.game.input.keyboard.createCursorKeys();		this.game.camera.follow(this.wolf);	},		update: function(){				this.game.physics.arcade.collide(this.hund, this.unten);		this.game.physics.arcade.collide(this.hund, this.end);		this.game.physics.arcade.collide(this.hund, this._hindernis);		this.game.physics.arcade.collide(this.schaaf, this.end, function(){			this.reverse = true;			console.log("jetzt");		});				if(this.reverse === false)			this.playerWolf();		else			this.playerHund();				console.log(this.reverse);				}, //ENDE UPDATE		---[some hindernis-stuff]---	},		playerWolf: function(){		this.game.physics.arcade.collide(this.wolf, this.unten);		this.game.physics.arcade.collide(this.wolf, this.end);		this.game.physics.arcade.collide(this.wolf, this._hindernis);		this.game.physics.arcade.collide(this.schaaf, this.unten);		this.game.physics.arcade.collide(this.schaaf, this._hindernis, function(schaaf){     		schaaf.body.velocity.x = 0;     		schaaf.body.velocity.y = -300;		});		this.game.physics.arcade.collide(this.wolf, this.schaaf, function(wolf,schaaf){			console.log("Schaf bekommt aufs Maul");			schaaf.body.velocity.y = -500;		});						if (this.cursors.left.isDown) {        	this.wolf.body.velocity.x = -200;		} else if (this.cursors.right.isDown) {			this.wolf.body.velocity.x = 200;		} else {			this.wolf.body.velocity.x = 0;		} if (this.cursors.up.isDown && this.wolf.body.wasTouching.down) {			this.wolf.body.velocity.y = -300;		}				this.schaaf.body.velocity.x = 180;	},			playerHund: function(){				this.game.physics.arcade.collide(this.wolf, this.unten);		this.game.physics.arcade.collide(this.wolf, this._hindernis, function(wolf){     		wolf.body.velocity.x = 0;     		wolf.body.velocity.y = -300;		});		this.game.physics.arcade.collide(this.hund, this.wolf, function(hund,wolf){			console.log("wolf bekommt aufs Maul");			wolf.body.velocity.y = -500;		});		this.game.physics.arcade.collide(this.wolf, this.end, function(){			 console.log("spiel zuende");		});				if (this.cursors.left.isDown) {        	this.hund.body.velocity.x = -220;		} else if (this.cursors.right.isDown) {			this.hund.body.velocity.x = 220;		} else {			this.hund.body.velocity.x = 0;		} if (this.cursors.up.isDown && this.hund.body.wasTouching.down) {			this.hund.body.velocity.y = -300;		}				this.wolf.body.velocity.x = -200;	}			};
Link to comment
Share on other sites

A state in Phaser(or Flixel) is what you would call a scene in other engines so it wouldn't be the right approach for what you want to achive. 

The most straightforward way I can think code based on your code would be to check the value of this.reverse inside playerHund and playerWolf to decide if they're gonna react to input or not. That way you can still update them(they may still animate, or react to damage, etc) but they'll ignore all input when not being controlled. 

 

Also, in:

 

 this.game.physics.arcade.collide(this.schaaf, this.end, function(){

            this.reverse = true;
            console.log("jetzt");
        });

 

you should add one last argument: 

 

 this.game.physics.arcade.collide(this.schaaf, this.end, function(){

            this.reverse = true;
            console.log("jetzt");
        }, this);

 

That would ensure that the this in this.reverse refers to what you want it to refer to. If you don't know how this works in javascript I suggest you read https://github.com/getify/You-Dont-Know-JS/blob/master/this%20&%20object%20prototypes/ch1.md

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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