【问题标题】:Tweened Kinetic.js shape doesn't fire most of clicks补间 Kinetic.js 形状不会触发大多数点击
【发布时间】:2014-11-16 10:40:14
【问题描述】:

目的

我正在制作一个简单的“射击文字”游戏,用户需要点击一些带有文字的移动矩形来“射击”它们。

问题

所以我创建了一些对象并使用简单的 kinetic.js 补间移动它们。

造字

函数 createWord(value){

//here comes some word object construction
var wordGroup = new Kinetic.Group({
    x: 0,
    y: 0
});
  var padding = 10;

wordGroup.label = new Kinetic.Text({
    x: padding,
    y: padding,
    text: value,
    fontFamily: 'Times New Roman',
    fontSize: 30,
    fill: 'white'
});

wordGroup.tag = new Kinetic.Rect({
    x: 0,
    y: 0,
    width: wordGroup.label.width() + (padding << 1),
    height: wordGroup.label.height() + (padding << 1),
    fill: 'black',
    shadowColor: 'black',
    shadowBlur: 10,
    shadowOffset: {x:10,y:20},
    shadowOpacity: 0.5,
    cornerRadius: 10
});

wordGroup.add(wordGroup.tag);
wordGroup.add(wordGroup.label);

wordGroup.shoot = function(){ //shooting mechanism (simple stop from moving and remove from scene)
    wordGroup.tween.pause();
    wordGroup.clean();
      dropNextWord(); //drops fresh blood! (new word instead of shooted)
}

wordGroup.clean = function(){ //remove from scene and set it free to drop again
   wordGroup.remove();
   wordGroup.isActive = false;   
}

wordGroup.move = function(callback){ //animates word
    wordLayer.add(wordGroup);        
    moveToSide(wordGroup, callback); //calls moving function
}

wordGroup.on('click', function(e){
    wordGroup.shoot();
});

return wordGroup; 
}

补间部分

//move word to opposite side
function moveToSide(word, callback){
    var side = Math.random();

    var d = 100;

    spawnFromSide(word, side); //set random side word position

    tweenPosition = {
        x: word.x(),
        y: word.y()
    }

    if(side < 0.25){ //left
        tweenPosition.x = - d;
    } else if(side > 0.25 && side < 0.5){ //right
        tweenPosition.x = defaultStageWidth + d;
    } else if(side > 0.5 && side < 0.75){ //up
        tweenPosition.y = - d;
    } else { //down
        tweenPosition.y = defaultStageHeight + d;
    }

    word.tween = new Kinetic.Tween({
        node: word,
        duration: 4,
        easing: Kinetic.Easings.Linear,
        x: tweenPosition.x,
        y: tweenPosition.y,
        onFinish: function(){
            word.clean();
            callback();
        }
    });

    word.tween.play();
}

但问题是点击事件不会在大量用户点击时触发。正如我认为的那样,这是由补间机制内部的延迟 drawHit() 调用引起的,它在更新命中区域之前绘制新的对象位置,所以当我们拍摄对象认为我们击中了它的当前位置时,我们错过了,因为它的命中区域仍然具有相同的旧位置位置。

活生生的例子

http://jsfiddle.net/hd6z21de/7/

花一分钟的时间来看看这个效果

【问题讨论】:

    标签: javascript html html5-canvas kineticjs tween


    【解决方案1】:

    通过监听画布触摸解决了这个奇怪的行为,并检查指针是否由我自己碰撞一些目标单词-rect,而不是使用他们自己的 onclick 事件。

    //由于我的应用特定,我听画布,你可以简单地听你自己的层甚至文档

    $("canvas").bind('click', function(event){

            var x = (event.pageX) / stage.scaleX(); //you don't need to divide by scale if your stage isn't scaled as mine does
            var y = (event.pageY) / stage.scaleY();
    
            var wordArray = wordGroup.getChildren();
    
            for(var i = 0; i < wordArray.length; i++){ //go through all words and check if we shoot someone (is mouse position included in some word rect)
                if(x > wordArray[i].x() &&
                y > wordArray[i].y() &&
                x < (wordArray[i].x() + wordArray[i].width()) &&
                y < (wordArray[i].y() + wordArray[i].height())){
                    wordArray[i].shoot(); //shoot that word
                    break;
                }    
            }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2015-11-20
      • 2020-12-27
      • 1970-01-01
      • 2018-04-18
      相关资源
      最近更新 更多