i am making a game in which i want to have a "player" object, and i want to do this in a function, but i also want to bind the object to the image displayed as the player. i want this because i later create monsters, and i want the hover to have a similar effect with a hover, but be based on that object's property's (i.e. display their health). my current method does not use .data(), and employ's backwards coding, while it works fine for what i have now, i cant expand my game with this code so i want to fix it up.
here is my attempt
function Player() {
this.currentLocation = 0;
printPlayer(this);
}
function printPlayer(p) {
$("#" + p.currentLocation).html( "<img src = 'http://3.bp.blogspot.com/-kAhN0HX-MBk/T_5bApfhbJI/AAAAAAAAAuI/lUww8xT9yV8/s1600/smileys_001_01.png'" +
"class='onTop displayCurrentHealth' alt='you' border='0' />" +
"<div class = 'healthLost hide'></div>");
}
var player = new Player();
console.log($("#" + player.currentLocation).data("inFight", "false"));
console.log($("#" + player.currentLocation).data("inFight"));
console.log($("#" + player.currentLocation).data(!"inFight"));
this is my console.log() result
Object[]
adventure.js (line 62)
null
adventure.js (line 63)
null
adventure.js (line 64)
here is my old code
function Player(id) {
this.id = id;
this.healthid = this.id + "health";
this.displayText = "<img src = 'http://3.bp.blogspot.com/-kAhN0HX-MBk/T_5bApfhbJI/AAAAAAAAAuI/lUww8xT9yV8/s1600/smileys_001_01.png'" +
"class='onTop displayCurrentHealth' id='" + this.id + "' alt='you' border='0' />" +
"<div id = '" + this.healthid + "' class = 'healthLost hide'></div>";
this.inFight = false;
this.currentLocation = 0;
this.xp = 0;
this.level = 1;
}
Object.defineProperty(player,"health",{
set: function() {
return 100 + ( this.level * 150 );
},
get: function() {
return 100 + ( this.level * 150 );
},
enumerable: true
} );
player.currentHealth = player.health;
Object.defineProperty(player,"strength",{
set: function() {
return ( this.level * 5 );
},
get: function() {
return ( this.level * 5 );
},
enumerable: true
} );
Object.defineProperty(player,"hitRating",{
set: function() {
return 3 + ( this.level );
},
get: function() {
return 3 + ( this.level );
},
enumerable: true
} );
how do i create that as jquery data(), dynamically in a function, onto a image that can have a changing location?
--update--
i added in this
$("#" + player.currentLocation).data("foo");
console.log($("#" + player.currentLocation).data());
which also returns null.
--update--
ok so i take it i need some sort of mvc, is there a way to do this in just javascript/jquery? and if yes how? please provide full explanation and/or links.
$("#" + p.currentLocation).html("<a new img>")dovar $player = $("<img id='player' ...>").data("whatever", value)and$("#" + p.currentLocation).append($player). Don't delete the<img>that represents the player. Just move it around. (Hint:.append()moves a node in the DOM since a node can only ever be appended to one parent.)