/    Sign up×
Community /Pin to ProfileBookmark

Object and Onclick Event

Hi all ? I’m trying to develop a tic tac toe game in js, using minmax algorithm for the AI.
But, as in the previous posts, the problem i have is about handling a class “inside” a function called by an event.
Uhm… probably the previous sentence is confusing, so let’s show the code ?

[code]
<html>
<head>
<script>

var n = 3;

function $(id){
var l = arguments.length;
var ref = new Array();
if(l > 1){
for(var i = 0; i < l; i++)
re.push(document.getElementById(arguments[i]));
return ref;
}
if(typeof id == “string”)
return document.getElementById(id);
return document;
}

function tictactoe(n){
this.n = n;
this.move = 0;

this.init = function(){
this.draw();
this.a = new Array();
for(var i = 0; i < this.n; i++)
for(var j = 0; j < this.n; j++)
this.a.push(0);
}

this.minmax = function(s){
var v = maxv(s);
return 0;
}

this.calculate = function(){
this.src = (this.move%2) ? ‘x.jpg’:’o.jpg’;
this.move += 1;
}

this.draw = function(){
for(var i = 0; i <this.n; i++){
for(j = 0; j < this.n; j++){
var img = document.createElement(‘img’);
img.setAttribute(‘id’,i*n+j);
img.setAttribute(‘src’,’e.jpg’);
img.onclick = this.calculate;
if (img.captureEvents) img.captureEvents(Event.CLICK);
$(‘container’).appendChild(img);
}
var br = document.createElement(‘br’);
$(‘container’).appendChild(br);
}
}
}
</script>
</head>
<body>
<div id=’container’>
</div>
<script>
var t = new tictactoe(3);
t.init();
</script>
</body>
</html>
[/code]

The problem is,

[code]this.move += 1[/code]

called in this.calculate, the function called by the onclick event, doesn’t have any effect on the this.move element of the Map object.
How can i modify his value?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@iokosanauthorFeb 14.2009 — I've found a "not so elegant" solution:
<i>
</i> this.calculate = function(){
this.src = (this.caller.move%2) ? 'x.jpg':'o.jpg';
this.caller.move += 1;
}
this.draw = function(){
for(var i = 0; i &lt;n; i++){
for(j = 0; j &lt; n; j++){
var img = document.createElement('img');
img.setAttribute('id',i*n+j);
img.setAttribute('src','e.jpg');
img.caller = this;
img.onclick = this.calculate;
if (img.captureEvents) img.captureEvents(Event.CLICK);
$('container').appendChild(img);
}
var br = document.createElement('br');
$('container').appendChild(br);
}
}


Someone has better ideas?
Copy linkTweet thisAlerts:
@web_bertFeb 14.2009 — Your problem seems to be with scoping. 'this' inside of the calculate function will = the img that is clicked on. So you need to capture the current scope of 'this' and then call the calculate function on that:

[CODE]
this.draw = function(){
var that = this;
for(var i = 0; i <this.n; i++){
for(j = 0; j < this.n; j++){
var img = document.createElement('img');
img.setAttribute('id',i*n+j);
img.setAttribute('src','e.jpg');
img.onclick = function(){
that.calculate();
};
if (img.captureEvents) img.captureEvents(Event.CLICK);
$('container').appendChild(img);
}
var br = document.createElement('br');
$('container').appendChild(br);
}
}

[/CODE]
×

Success!

Help @iokosan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...