You must assign a class to the library symbol before you can use that code. Until you do, Truck is undefined.
Where did you define the tween which animates the truck? It cannot be on the individual truck_mc and truck2_mc, or else the new Trucks won't have it. Given what you've said, here's how I would make a bunch of trucks at random and move them across the screen, checking for collisions. This code is untested.
Code:var trucks:Array = new Array(); var maxDelay:Number = 5000; //5 seconds var timer:Timer = new Timer(Math.random()*maxDelay); timer.addEventListener(TimerEvent.TIMER, launchTruck); timer.start(); function launchTruck(e:TimerEvent):void{ timer.delay = Math.random()*maxDelay; var truck:Truck = new Truck(); truck.y = Math.random()*stage.stageHeight; trucks.push(truck); addChild(truck); } addEventListener(Event.ENTER_FRAME, checkFrame); function checkFrame(e:Event):void{ var hit:Boolean = false; var toremove:Array = new Array(); for (var i:int = 0; i < trucks.length; i++){ var t:Truck = trucks[i]; t.x += 5; //move truck to the right. Adjust to change speed. if (chevelle_mc.hitTestObject(t)) { chevelle_mc.stopDrag(); hit = true; toremove.push(t); break; //stop the loop because chevelle already hit a truck. } if (t.x > stage.stageWidth){ toremove.push(t); } } removeTrucks(toremove); if (hit){ gotoAndPlay(4); } } function removeTrucks(toremove:Array):void{ while (toremove.length > 0){ var t:Truck = toremove.pop(); removeChild(t); trucks.splice(trucks.indexOf(t), 1); //remove it from the trucks array too. } }




Reply With Quote