我有一张地图,我希望能够滚动。滚动工作,但现在因为地图在我的菜单下,点击菜单中某些内容的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);
}
感谢您的阅读。
答案 0 :(得分: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()
});