1 2 3 4 5 6 7 8 9 | var SnailBait = function () { ... isCandidateForCollision: function (sprite, otherSprite) { return sprite !== otherSprite && // not same sprite.visible && otherSprite.visible && // visible !sprite.exploding && !otherSprite.exploding; // not exploding }, ... }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | var SnailBait = function () { ... didCollide: function (sprite, // runner otherSprite, // candidate for collision context) { // for context.isPointInPath() var left = sprite.left + sprite.offset, right = sprite.left + sprite.offset + sprite.width, top = sprite.top, bottom = sprite.top + sprite.height, centerX = left + sprite.width/2, centerY = sprite.top + sprite.height/2; // All the preceding variables -- left, right, etc. -- pertain to the runner sprite. if (otherSprite.type !== 'snail bomb') { return this.didRunnerCollideWithOtherSprite(left, top, right, bottom, centerX, centerY, otherSprite, context); } else { return this.didSnailBombCollideWithRunner(left, top, right, bottom, otherSprite, context); } }, |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | didRunnerCollideWithOtherSprite: function (left, top, right, bottom, centerX, centerY, otherSprite, context) { // Determine if either of the runner's four corners or its // center lies within the other sprite's bounding box. context.beginPath(); context.rect(otherSprite.left - otherSprite.offset, otherSprite.top, otherSprite.width, otherSprite.height); return context.isPointInPath(left, top) || context.isPointInPath(right, top) || context.isPointInPath(centerX, centerY) || context.isPointInPath(left, bottom) || context.isPointInPath(right, bottom); }, |
1 2 3 4 5 6 7 8 9 10 11 12 | didSnailBombCollideWithRunner : function (left, top, right, bottom, snailBomb, context) { // Determine if the center of the snail bomb lies within // the runner's bounding box context.beginPath(); context.rect(left, top, right - left, bottom - top); // runner's bounding box return context.isPointInPath( snailBomb.left - snailBomb.offset + snailBomb.width/2, snailBomb.top + snailBomb.height/2); }, |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |