1

我正在制作一个游戏,其中我想要一个“玩家” object,我想在一个函数中做到这一点,但我也想将对象绑定到显示为player. 我想要这个,因为我后来创建了怪物,我希望悬停与悬停具有类似的效果,但基于该object属性(即显示它们的健康)。我当前的方法不使用.data(),并使用反向编码,虽然它适用于我现在拥有的东西,但我无法使用此代码扩展我的游戏,所以我想修复它。

这是我的尝试

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"));

这是我的console.log()结果

Object[]
adventure.js (line 62)
null
adventure.js (line 63)
null
adventure.js (line 64)

这是我的旧代码

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 
} );

我如何将它作为 jquerydata()在函数中动态创建到可以具有更改位置的图像上

- 更新 -

我添加了这个

$("#" + player.currentLocation).data("foo");
console.log($("#" + player.currentLocation).data());

这也返回null

- 更新 -

好的,所以我认为我需要某种mvc,有没有办法在 javascript/jquery 中做到这一点?如果是的话怎么办?请提供完整的解释和/或链接。

4

2 回答 2

1

正如评论中所指出的,保留数据的关键是保留它所附加的 DOM 节点。

一种开始方式可能如下所示(从您的代码开始,进行尽可能小的更改):

function Player() {
    var self = this;

    self.location = 0;
    self.level = 1;
    self.xp = 0;
    self.inFight = false;

    // add methods that calculate XP and so on
}

function Game() {
    var self = this,
        player = new Player(),
        $playerSprite = $("<img>", {
            "class": "onTop displayCurrentHealth",
            "src": "http://3.bp.blogspot.com/-kAhN0HX-MBk/T_5bApfhbJI/AAAAAAAAAuI/lUww8xT9yV8/s1600/smileys_001_01.png",
            "alt": "you"
        }).data("model", player);

    self.printPlayer = function () {
        $("#" + player.location).append($playerSprite);
    };

    // init
    self.printPlayer();
}
于 2013-09-03T19:37:17.597 回答
1

不要附加data()到任何 DOM 节点。你应该有一个知道它的 DOM 节点的对象,以及一个Model. 你的怪物也可以访问,Model这样他们就知道玩家还剩下多少生命值。

您很少需要使用它data()来传递信息。这将使您的应用程序更新变得更加困难,特别是如果您想稍后调整 DOM。

保持您的 DOM 和业务逻辑尽可能分开以保持灵活性。

var Model = Function( id ) {
 this.health = 100;
 this.id = id;
};

// This object is showing examples of different ways an object can know about the image
var Monster = Function( hero_model, monster_model ) {
  this.sprite = $('#element').tmpl({}); // using jQuery template
  this.sprite = $('<img src="">'); // if you are making image based on model data
  this.sprite = $('#img-' + hero_model.id ); // if you wanted to  look on DOM then, based on data passed in
  this.sprite = document.getElementById( 'img-' + hero_model.id ); // to get element without jquery
};

var Player = Function( model ) {
 this.model = model;
}


// Passing 1 as ID, but you can have a number that increments
var hero_model = new Model( 1 );
hero_model.damage = 50;

var monster_model = new Model( 1 );
monster_model.damage = 10;

var hero = new Player( hero_model );

var monster = new Monster( hero_model, monster_model );
于 2013-09-02T17:25:47.113 回答