0

我有一个我希望能够滚动的地图。滚动工作,但现在因为地图在我的菜单下,用于单击菜单中某些内容的 mousedown 事件会触发该侦听器和地图拖动侦听器,因为地图超出它的图层大小并在菜单下。有没有办法限制这个?

代码:

//set main namespace
goog.provide('rh.game');

//get requirements
goog.require('lime.Director');
goog.require('lime.Scene');
goog.require('lime.Layer');
goog.require('lime.RoundedRect');
goog.require('lime.Sprite');
goog.require('lime.Label');
goog.require('rh.house');
// entrypoint
rh.start = function(){

var director = new lime.Director(document.body,1024,768).setDisplayFPS(false),
Game = new lime.Scene(),
menulayer = new lime.Layer().setAnchorPoint(0,0).setPosition(0,568),
menubg = new lime.RoundedRect().setAnchorPoint(0,0).setSize(1024,200).setFill('assets/menubg.png'),
houselbl = new lime.Sprite().setPosition(0, 0).setAnchorPoint(0,0).setFill('assets/storage.png');
flayer = new lime.Layer().setAnchorPoint(0,0).setPosition(0,0).setSize(1024,568),
field = new lime.Sprite().setSize(3000,2500).setAnchorPoint(0,0).setFill('assets/fields.jpg'),
selected_ = -1;

menulayer.appendChild(menubg);
menulayer.appendChild(houselbl);
flayer.appendChild(field);

goog.events.listen(flayer,['mousedown'],function(e){
    if(selected_ == 1){
    var house = new rh.house().setPosition(e.position.x, e.position.y);
    flayer.appendChild(house);
    selected_ = -1;
    houselbl.setFill('assets/storage.png');
    }


    e.startDrag(false, new goog.math.Box(-1932, 0, 0, -1976));      
});

goog.events.listen(houselbl,['mousedown'],function(e){
    selected_ = 1
    houselbl.setFill('assets/storages.png');
});

Game.appendChild(flayer);
Game.appendChild(menulayer);

director.replaceScene(Game);
}

感谢您的阅读。

4

1 回答 1

0

将 e.event.stopPropagation() 添加到侦听器修复它以阻止它访问其他对象,而不是重叠时指向的对象。

例子:

goog.events.listen(houselbl,['mousedown'],function(e){
    e.swallow(['mouseup'],function(){

    if (selected_ !== -1) {
        selected_ = -1;
        houselbl.setFill('assets/storage.png');
    }else{
    selected_ = 1
    houselbl.setFill('assets/storages.png');
    }
    });
e.event.stopPropagation()
});
于 2012-10-17T09:36:46.563 回答