Bonsoir ,

je fais appel à vous car je suis coincée sur mon code depuis plusieurs heures, je n'arrive pas à trouver la faille.
Il s'agit de récupérer la position en x ou en y de mon personnage à l'aide de console.log(pers1.posX), où pers1 est l'objet instanciée de la fonction Personnage et posX, un attribut de Personnage donc de pers1. Mon problème est que j'obtiens la valeur 300 qui est la valeur initiale de posX. Je ne comprends pas pourquoi je n'arrive pas à obtenir la position en x réelle de mon personnage???


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
 
 
	function Personnage(){
 
		this.posX = 300;
		this.posY = 395;
		this.left = false;
		this.right = false;
		this.up = false;
		this.down= false;
		this.g;
		this.image = new Image();
		this.image.src = 'pers.png';
 
	}
 
 
 
 
	Personnage.prototype.keyBoard = function(){
 
		var self = this;
 
		document.addEventListener('keydown', function(event){
			var key_pressed; 
 
			if(event == null) key_pressed = window.event.keyCode; 
 
			else key_pressed = event.keyCode; 
 
 
			switch(key_pressed)
			{
				case 37:
					self.left=true;    
					break; 
 
				case 38:
					self.up=true;      
					break; 
 
				case 39:
					self.right=true;   
					break;
 
				case 40:
					self.down=true;   
					break; 
			} 		
 
		},false);
 
 
		document.addEventListener('keyup', function(event){
			var key_pressed; 
 
			if(event == null) key_pressed = window.event.keyCode; 
 
			else key_pressed = event.keyCode; 
 
 
			switch(key_pressed)
			{
				case 37:
					self.left=false;       
					break;  
 
				case 38:
					self.up=false;        
					break; 
 
				case 39:
					self.right=false;     
					break;
 
				case 40:
					self.down=false;      
					break; 
			} 		
 
 
		},false);
	}
 
 
 
 
	Personnage.prototype.personnageMovment= function(ctx)
	{
 
		if (this.left)                                                
		{
			ctx.clearRect(0, 0, 650, 450);
			ctx.drawImage(this.image, 710, 214, 29, 25, this.posX, this.posY, 29, 25);
			this.posX -= 2.5;
			this.g=0;
		}
 
		if (this.left==false && this.g==0)                           
		{                                                               
				ctx.clearRect(0, 0, 650, 450);                          
				ctx.drawImage(this.image, 434, 211, 21, 26, this.posX, this.posY, 21, 26);
		}
 
		if (this.up)                                                  
		{
			ctx.clearRect(0, 0, 650, 450);
			ctx.drawImage(this.image, 522, 209, 25, 29, this.posX, this.posY, 25, 29);
			this.posY -= 2.5;	
			this.g=1;	
		}
 
		 if (this.up==false && this.g==1)                              
		{                                                               
			ctx.clearRect(0, 0, 650, 450);
			ctx.drawImage(this.image, 393, 214, 27, 20, this.posX, this.posY, 27, 20);
		}
 
		if (this.right)                                                  
		{
			ctx.clearRect(0, 0, 650, 450);
			ctx.drawImage(this.image, 646, 212, 29, 25, this.posX, this.posY, 29, 25);
			this.posX += 2.5;
			this.g=2;
		}
 
		if (this.right==false && this.g==2)                              
		{                                                                
			ctx.clearRect(0, 0, 650, 450);
			ctx.drawImage(this.image, 473, 211, 21, 26, this.posX, this.posY, 21, 26);
		}
 
		if (this.down)                                                    
		{
			ctx.clearRect(0, 0, 650, 450);
			ctx.drawImage(this.image, 584, 209, 25, 29, this.posX, this.posY, 25, 29);
			this.posY += 2.5;
			this.g=3;
		}
 
		if (this.down==false && this.g==3)                                
		{                                                                 
			ctx.clearRect(0, 0, 650, 450);
			ctx.drawImage(this.image, 347, 214, 27, 20, this.posX, this.posY, 27, 20);
		}
 
 
	}
 
 
 
 
	Personnage.prototype.personnageAnimate = function(){
 
		var self = this;
			this.image.onload = function(){
				setInterval(function(){self.personnageMovment(ontext)}, 10);
			}
 
	}
 
 
 
 
	var pers1= new Personnage();
	pers1.keyBoard();
	pers1.personnageAnimate();
 
	console.log(pers1.posX);