Jump to content

help with prototype


espace
 Share

Recommended Posts

hi,

i have the error undefined at line :         console.log(this.sprite_for_drag.x,"here") in the update function but i don't understand why. Could you please help me a little bit.

 


<!DOCTYPE HTML>
<html>
	<head> 
		<title>BubX</title>
		<link rel="shortcut icon" href="assets/favicon.png" type="image/x-icon" />
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="chrome=1, IE=9">
		<meta name="format-detection" content="telephone=no">
		<meta name="HandheldFriendly" content="true" />
		<meta name="robots" content="noindex,nofollow" />
		<meta name="apple-mobile-web-app-capable" content="yes" />
		<meta name="apple-mobile-web-app-status-bar-style" content="black" />
		<meta name="apple-mobile-web-app-title" content="Phaser App"> 
	</head> 

	<script src="phaser.min.js"></script>
	<script src="src/main.js"></script>

	<style>
body {

	margin: auto;
	display: table;
	position: absolute;
	border:0px;
	top: 0px;
	left: 0px;
	padding: 0; margin: 0;
	background-color: #0d1018
}
	</style>
	</body>
	<script>
(function() {

	var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
	//class for mechant
	_mechant = function(name,number,posx,posy,image){
		this.name=name
		this.number=number
		this.image=image
		this.posx=posx
		this.posy=posy
		this.sprite_for_drag=game.add.sprite(this.posx,this.posy,'sprite_for_drag')
		this.sprite_for_drag.anchor.setTo(.5,.5)
		this.sprite_for_drag.inputEnabled=true
		this.sprite_for_drag.input.enableDrag(true)
		this.sprite_for_drag.input.enableSnap(40,40,true,true)
		this.sprite_for_body=game.add.sprite(this.posx,this.posy,this.image)
		this.sprite_for_body.anchor.setTo(.5,.5)
		game.physics.arcade.enable(this.sprite_for_body)
		this.sprite_for_body.immovable=true
		this.flag=true
		game.add.existing(this.sprite_for_drag)
		game.add.existing(this.sprite_for_body)
		game.time.events.loop(150,this.update)
		console.log(this.sprite_for_body.x,"fjhjksd")
	}

	_mechant.prototype.update=function(){
		console.log(this.sprite_for_drag.x,"here")
		this.sprite_for_body.x=this.sprite_for_drag.x
		this.sprite_for_body.y=this.sprite_for_drag.y
	}

	function preload() {
		game.load.image('circle', 'https://s13.postimg.org/xjhlzmiev/disc_png.png');
	}

	function create() {
		var c=game.add.sprite(100,100,'circle')
			var e= new _mechant("mech",5,300,300,'circle')
	}

	function update() {
	}

	function render() {
	}

})();
	</script>
</html>

 

Link to comment
Share on other sites


	_mechant.prototype.update=function(this){
		console.log(this.sprite_for_drag.x,"here")
}



Hi, thanks for the reply. Why must I specify this in the update function ? Normally prototype refer to this so this.sprite_for_drag is concern ?

Link to comment
Share on other sites

solved

var game = new Phaser.Game(800, 600,Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });

//class for mechant
_mechant = function(game,name,number,posx,posy,image){
	Phaser.Sprite.call(this,game,0,0,this.image)
	this.name=name
	this.number=number
	this.image=image
	this.posx=posx
	this.posy=posy
	this.anchor.setTo(.5,.5)
	this.inputEnabled=true
	this.input.enableDrag(true)
	this.input.enableSnap(40,40,true,true)
	this.sprite_for_body=game.add.sprite(this.posx,this.posy,this.image)
	this.sprite_for_body.anchor.setTo(.5,.5)
	game.physics.arcade.enable(this.sprite_for_body)
	this.sprite_for_body.immovable=true
	this.flag=true
}
_mechant.prototype=Object.create(Phaser.Sprite.prototype)
//not necessary
//_mechant.constructor=_mechant

_mechant.prototype.hide=function(){
	this.tween1=game.add.tween(this.scale).to({x:0,y:0},time_hide,Phaser.Easing.Bounce.In,true,0)
	this.tween2=game.add.tween(this.sprite_for_body.scale).to({x:0,y:0},time_hide,Phaser.Easing.Bounce.In,true,0)
	this.sprite_for_body.enable=false
	this.tween1.onComplete.add(function(){this.visible=false;this.inputEnabled=false},this)
	this.tween2.onComplete.add(function(){this.sprite_for_body.visible=false},this)
}

_mechant.prototype.update=function(){
	console.log("here")
	console.log(this.x,"here")
	this.sprite_for_body.x=this.x
	this.sprite_for_body.y=this.y
}

function preload() {
	game.load.image('circle', 'https://s13.postimg.org/xjhlzmiev/disc_png.png');
}

function create() {
	var e= new _mechant(game,"mech",5,300,300,'circle')
	game.add.existing(e)
}

function update() {
}

function render() {
}

sorry for the indentation, when i use vim (the best IDE :ph34r:) with html file it's not good but with js file no problem (==). in fact my problem is due to the fact that this is not assigned really, it was this.something who was declared. thanks ;)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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