You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AS3 Error #1010问题求助:披萨与吃豆人碰撞检测异常

Hey Stanley, let's work through your Pizza Delivery game issues one by one—first squashing that annoying TypeError, then fixing the flaky collision detection.

Step 1: Fix the TypeError #1010

That error means your update() method is trying to access a property of an undefined or null object. This is almost certainly why your collision detection only works 40% of the time—when the error hits, the rest of your update logic (including collision checks) stops running entirely.

Here are the most likely culprits and fixes:

  • Pizza instances are being removed but still referenced: If pizzas disappear (from going off-screen or collision) but you don't properly remove them from your update loop, your code will try to access properties like x or y on a dead object.
    • If you're using an array to track pizzas, always iterate backward when removing elements to avoid index gaps:
      for(var i:int = pizzas.length - 1; i >= 0; i--) {
          var pizza:Pizza = pizzas[i];
          // Check if the pizza is no longer in the display list
          if(pizza == null || pizza.parent == null) {
              pizzas.splice(i, 1);
              continue;
          }
          // Rest of your update logic here
      }
      
  • Missing null checks before accessing properties: Add guards anywhere you access pizza or pacman properties in update():
    if(pizza && pacman) { // Ensure both objects exist
        if(pizza.hitTestObject(pacman)) {
            // Collision logic
        }
    }
    
  • Forgot to remove event listeners: When you delete a pizza, make sure to remove its ENTER_FRAME listener first, otherwise the dead pizza will still trigger update():
    pizza.removeEventListener(Event.ENTER_FRAME, update);
    if(pizza.parent) pizza.parent.removeChild(pizza);
    
Step 2: Fix Collision Detection Inconsistencies

Once the TypeError is fixed, let's tackle the pizza passing through Pac-Man or vanishing randomly:

1. Ensure collision checks run every frame

If you're only checking collision when the pizza is first thrown, it'll miss Pac-Man moving into the pizza's path. Move your collision detection into the update() method so it runs every frame.

2. Fix fast-moving pizza skipping collision

If your pizza moves too far per frame, it can jump right through Pac-Man's hitbox. Fix this with stepwise collision detection: split the pizza's movement into smaller steps and check collision at each step:

// In your pizza's update logic
var totalMoveX:Number = pizza.speed * Math.cos(pizza.rotation * Math.PI/180);
var totalMoveY:Number = pizza.speed * Math.sin(pizza.rotation * Math.PI/180);

// Split movement into 5 small steps (adjust based on speed)
var steps:int = 5;
var stepX:Number = totalMoveX / steps;
var stepY:Number = totalMoveY / steps;

for(var s:int = 0; s < steps; s++) {
    pizza.x += stepX;
    pizza.y += stepY;
    // Check collision at each step
    if(pizza.hitTestObject(pacman)) {
        // Trigger collision (destroy pizza, score point, etc.)
        break;
    }
}

3. Verify hitbox accuracy

hitTestObject() uses the MovieClip's bounding box, which might not match the visual pizza/Pac-Man if their registration points are off. Try using getBounds() for more precise rectangle overlap checks:

import flash.geom.Rectangle;

var pizzaBounds:Rectangle = pizza.getBounds(stage);
var pacmanBounds:Rectangle = pacman.getBounds(stage);

if(pizzaBounds.intersects(pacmanBounds)) {
    // Collision detected
}

4. Fix random pizza vanishing

Pizzas disappearing for no reason usually means your off-screen removal logic is too aggressive, or you're deleting pizzas without cleaning up references. Double-check your out-of-bounds check:

if(pizza.x < -pizza.width || pizza.x > stage.stageWidth + pizza.width || 
   pizza.y < -pizza.height || pizza.y > stage.stageHeight + pizza.height) {
    // Only remove pizza when it's fully off-screen
    pizza.removeEventListener(Event.ENTER_FRAME, update);
    if(pizza.parent) pizza.parent.removeChild(pizza);
    var index:int = pizzas.indexOf(pizza);
    if(index != -1) pizzas.splice(index, 1);
}
Final Tip

Start with fixing the TypeError first—once that's resolved, your collision detection will at least run consistently, making it easier to debug the remaining issues. Test each fix incrementally so you can pinpoint exactly what's working.

内容的提问来源于stack exchange,提问作者Stanley Tang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:22:30