我能够让我的青蛙从日志跳到日志,上升,完美无缺。 虽然,当我试图跳下去时,我的青蛙会忽略与下面的日志的碰撞而只是站在那里。
我正处于查看此代码让我头疼的地步。如果你们可以帮助我,或者只是指出我正确的方向,我一定会非常感激。非常感谢你!
抱歉我的代码太大了。所需的一切都将位于第一个CharMove功能
中 package {
import flash.events.Event
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class FroggerClass extends MovieClip
{
var lane1s, lane2s, lane3s, lane4s:int; //Car Lanes Speed
var lane5s, lane6s, lane7s, lane8s, lane9s:int; //Log Lanes Speed
//Checks if dead to determine when to revive
var death:Boolean;
//Helps check if on/off log
var lg1, lg2, lg3, lg4, lg5, lg6, lg7:Boolean;
var lg8, lg9, lg10, lg11, lg12, lg13, lg14: Boolean;
public function FroggerClass()
{
Frog1.gotoAndStop(1);
init();
//Initializes speed for each lane
lane1s = 2;
lane2s = 5;
lane3s = 3;
lane4s = 6;
lane5s = 3;
lane6s = 5;
lane7s = 2;
lane8s = 7;
lane9s = 6;
//Default setting
death = false;
lg1, lg2, lg3, lg4, lg5, lg6, lg7 = false;
lg8, lg9, lg10, lg11, lg12, lg13, lg14 = false;
}
function init():void
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, CharMove);
stage.addEventListener(KeyboardEvent.KEY_UP, CharStop);
addEventListener(Event.ENTER_FRAME, ObsLoop);
addEventListener(Event.ENTER_FRAME, HitDie);
addEventListener(Event.ENTER_FRAME, Revive);
}
function CharMove(event:KeyboardEvent):void
{
var playerHalfWidth:uint = Frog1.width / 2;
var playerHalfHeight:uint = Frog1.height / 2;
switch (event.keyCode)
{
case Keyboard.UP:
Frog1.y -= 30;
Frog1.gotoAndStop(2);
//trace(Frog1.y);
break;
case Keyboard.DOWN:
Frog1.y +=30;
Frog1.gotoAndStop(3);
//trace(Frog1.y);
break;
case Keyboard.RIGHT:
Frog1.x += 20;
Frog1.gotoAndStop(10);
break;
case Keyboard.LEFT:
Frog1.x -= 20;
Frog1.gotoAndStop(9);
break;
}
//Keeps Frog within boundaries of the stage
if(Frog1.x + playerHalfWidth > stage.stageWidth)
{
Frog1.x = stage.stageWidth - playerHalfWidth;
}
else if (Frog1.x - playerHalfWidth < 0)
{
Frog1.x = 0 + playerHalfWidth;
}
if(Frog1.y - playerHalfHeight < 0)
{
Frog1.y = 0 + playerHalfHeight;
}
else if(Frog1.y + playerHalfHeight > stage.stageHeight)
{
Frog1.y = stage.stageHeight - playerHalfHeight;
}
//----------ON OR OFF LOG?---------------------------------//
if(!lg1 && Frog1.hitTestObject(Log1))
{
Log1.addChild(Frog1);
lg1 = true;
Frog1.x = 0;
Frog1.y = 0
}
else if(lg1 && event.keyCode == Keyboard.UP)
{
stage.addChild(Frog1);
Frog1.x = (Log1.x);
Frog1.y = (Log1.y -30);
lg1 = false;
}
else if(lg1 && event.keyCode == Keyboard.DOWN)
{
stage.addChild(Frog1);
Frog1.x = (Log1.x);
Frog1.y = (Log1.y +30);
lg1 = false;
}
else if(lg1 && (event.keyCode == Keyboard.RIGHT || Keyboard.LEFT))
{
stage.addChild(Frog1);
death = true;
}
//~~~~~~~~~~~Log2~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
if(!lg2 && Frog1.hitTestObject(Log2))
{
Log2.addChild(Frog1);
lg2 = true;
Frog1.x = 0;
Frog1.y = 0
}
else if(lg2 && event.keyCode == Keyboard.UP)
{
stage.addChild(Frog1);
Frog1.x = (Log2.x);
Frog1.y = (Log2.y -30);
lg2 = false;
}
else if(lg2 && event.keyCode == Keyboard.DOWN)
{
stage.addChild(Frog1);
Frog1.x = (Log2.x);
Frog1.y = (Log2.y +30);
lg2 = false;
}
else if(lg2 && (event.keyCode == Keyboard.RIGHT || Keyboard.LEFT))
{
stage.addChild(Frog1);
death = true;
}
//~~~~~~~~~~~Log3~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
if(!lg3 && Frog1.hitTestObject(Log3))
{
Log3.addChild(Frog1);
lg3 = true;
Frog1.x = 0;
Frog1.y = 0
}
else if(lg3 && event.keyCode == Keyboard.UP)
{
stage.addChild(Frog1);
Frog1.x = (Log3.x);
Frog1.y = (Log3.y -30);
lg3 = false;
}
else if(lg3 && event.keyCode == Keyboard.DOWN)
{
stage.addChild(Frog1);
Frog1.x = (Log3.x);
Frog1.y = (Log3.y +30);
lg3 = false;
}
else if(lg3 && (event.keyCode == Keyboard.RIGHT || Keyboard.LEFT))
{
stage.addChild(Frog1);
death = true;
}
//~~~~~~~~~~~~~~~~Log4~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
if(!lg4 && Frog1.hitTestObject(Log4))
{
Log4.addChild(Frog1);
lg4 = true;
Frog1.x = 0;
Frog1.y = 0
}
else if(lg4 && event.keyCode == Keyboard.UP)
{
stage.addChild(Frog1);
Frog1.x = (Log4.x);
Frog1.y = (Log4.y -30);
lg4 = false;
}
else if(lg4 && event.keyCode == Keyboard.DOWN)
{
stage.addChild(Frog1);
Frog1.x = (Log4.x);
Frog1.y = (Log4.y +30);
lg4 = false;
}
else if(lg4 && (event.keyCode == Keyboard.RIGHT || Keyboard.LEFT))
{
stage.addChild(Frog1);
death = true;
}
//~~~~~~~~~~~~~~~~~~~Log5~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
if(!lg5 && Frog1.hitTestObject(Log5))
{
Log5.addChild(Frog1);
lg5 = true;
Frog1.x = 0;
Frog1.y = 0
}
else if(lg5 && event.keyCode == Keyboard.UP)
{
stage.addChild(Frog1);
Frog1.x = (Log5.x);
Frog1.y = (Log5.y -30);
lg5 = false;
}
else if(lg5 && event.keyCode == Keyboard.DOWN)
{
stage.addChild(Frog1);
Frog1.x = (Log5.x);
Frog1.y = (Log5.y +30);
lg5 = false;
}
else if(lg5 && (event.keyCode == Keyboard.RIGHT || Keyboard.LEFT))
{
stage.addChild(Frog1);
death = true;
}
//~~~~~~~~~~~~~~~~~~Log6~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
if(!lg6 && Frog1.hitTestObject(Log6))
{
Log6.addChild(Frog1);
lg6 = true;
Frog1.x = 0;
Frog1.y = 0
}
else if(lg6 && event.keyCode == Keyboard.UP)
{
stage.addChild(Frog1);
Frog1.x = (Log6.x);
Frog1.y = (Log6.y -30);
lg6 = false;
}
else if(lg6 && event.keyCode == Keyboard.DOWN)
{
stage.addChild(Frog1);
Frog1.x = (Log6.x);
Frog1.y = (Log6.y +30);
lg6 = false;
}
else if(lg6 && (event.keyCode == Keyboard.RIGHT || Keyboard.LEFT))
{
stage.addChild(Frog1);
death = true;
}
//~~~~~~~~~~~~~Log7~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
if(!lg7 && Frog1.hitTestObject(Log7))
{
Log7.addChild(Frog1);
lg7 = true;
Frog1.x = 0;
Frog1.y = 0
}
else if(lg7 && event.keyCode == Keyboard.UP)
{
stage.addChild(Frog1);
Frog1.x = (Log7.x);
Frog1.y = (Log7.y -30);
lg7 = false;
}
else if(lg7 && event.keyCode == Keyboard.DOWN)
{
stage.addChild(Frog1);
Frog1.x = (Log7.x);
Frog1.y = (Log7.y +30);
lg7 = false;
}
else if(lg7 && (event.keyCode == Keyboard.RIGHT || Keyboard.LEFT))
{
stage.addChild(Frog1);
death = true;
}
//~~~~~~~~~~~~~~~~~~~~Log8~~~~~~~~~~~~~~~~~~~~~~~//
if(!lg8 && Frog1.hitTestObject(Log8))
{
Log8.addChild(Frog1);
lg8 = true;
Frog1.x = 0;
Frog1.y = 0
}
else if(lg8 && event.keyCode == Keyboard.UP)
{
stage.addChild(Frog1);
Frog1.x = (Log8.x);
Frog1.y = (Log8.y -30);
lg8 = false;
}
else if(lg8 && event.keyCode == Keyboard.DOWN)
{
lg8 = false;
stage.addChild(Frog1);
Frog1.x = (Log8.x);
Frog1.y = (Log8.y +30);
}
else if(lg8 && (event.keyCode == Keyboard.RIGHT || Keyboard.LEFT))
{
stage.addChild(Frog1);
death = true;
}
}
function CharStop(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
Frog1.gotoAndStop(1);
break;
case Keyboard.DOWN:
Frog1.gotoAndStop(4);
break;
case Keyboard.RIGHT:
Frog1.gotoAndStop(8);
break;
case Keyboard.LEFT:
Frog1.gotoAndStop(7);
break;
}
}
function ObsLoop(event:Event):void
{
var oHalfWidth:uint = Car1.width / 2;
var oHalfHeight:uint = Car1.height / 2;
//-------------- Lane 1 -----------------------
Car1.x -= lane1s;
if (Car1.x - oHalfWidth > stage.stageWidth)
{
Car1.x = 0 - oHalfWidth;
}
else if (Car1.x + oHalfWidth < 0)
{
Car1.x = stage.stageWidth + oHalfWidth;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
Car2.x -= lane1s;
if (Car2.x - oHalfWidth > stage.stageWidth)
{
Car2.x = 0 - oHalfWidth;
}
else if (Car2.x + oHalfWidth < 0)
{
Car2.x = stage.stageWidth + oHalfWidth;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
Car3.x -= lane1s;
if (Car3.x - oHalfWidth > stage.stageWidth)
{
Car3.x = 0 - oHalfWidth;
}
else if (Car3.x + oHalfWidth < 0)
{
Car3.x = stage.stageWidth + oHalfWidth;
}
//-------------- Lane 2 -----------------------
Truck1.x += lane2s;
if (Truck1.x - oHalfWidth > stage.stageWidth)
{
Truck1.x = 0 - oHalfWidth;
}
else if (Truck1.x + oHalfWidth < 0)
{
Truck1.x = stage.stageWidth + oHalfWidth;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
Truck2.x += lane2s;
if (Truck2.x - oHalfWidth > stage.stageWidth)
{
Truck2.x = 0 - oHalfWidth;
}
else if (Truck2.x + oHalfWidth < 0)
{
Truck2.x = stage.stageWidth + oHalfWidth;
}
//-------------- Lane 3 -----------------------
Car4.x -= lane3s;
if (Car4.x - oHalfWidth > stage.stageWidth)
{
Car4.x = 0 - oHalfWidth;
}
else if (Car4.x + oHalfWidth < 0)
{
Car4.x = stage.stageWidth + oHalfWidth;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
Car5.x -= lane3s;
if (Car5.x - oHalfWidth > stage.stageWidth)
{
Car5.x = 0 - oHalfWidth;
}
else if (Car5.x + oHalfWidth < 0)
{
Car5.x = stage.stageWidth + oHalfWidth;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
Car6.x -= lane3s;
if (Car6.x - oHalfWidth > stage.stageWidth)
{
Car6.x = 0 - oHalfWidth;
}
else if (Car6.x + oHalfWidth < 0)
{
Car6.x = stage.stageWidth + oHalfWidth;
}
//-------------- Lane 4 -----------------------
Truck3.x += lane4s;
if (Truck3.x - oHalfWidth > stage.stageWidth)
{
Truck3.x = 0 - oHalfWidth;
}
else if (Truck3.x + oHalfWidth < 0)
{
Truck3.x = stage.stageWidth + oHalfWidth;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
Truck4.x += lane4s;
if (Truck4.x - oHalfWidth > stage.stageWidth)
{
Truck4.x = 0 - oHalfWidth;
}
else if (Truck4.x + oHalfWidth < 0)
{
Truck4.x = stage.stageWidth + oHalfWidth;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
Truck5.x += lane4s;
if (Truck5.x - oHalfWidth > stage.stageWidth)
{
Truck5.x = 0 - oHalfWidth;
}
else if (Truck5.x + oHalfWidth < 0)
{
Truck5.x = stage.stageWidth + oHalfWidth;
}
//-------------- Lane 5 -----------------------
Log1.x -= lane5s;
if (Log1.x - oHalfWidth > stage.stageWidth)
{
Log1.x = 0 - oHalfWidth;
}
else if(Log1.x + oHalfWidth < 0)
{
Log1.x = stage.stageWidth + oHalfWidth;
}
Log2.x -= lane5s;
if (Log2.x - oHalfWidth > stage.stageWidth)
{
Log2.x = 0 - oHalfWidth;
}
else if(Log2.x + oHalfWidth < 0)
{
Log2.x = stage.stageWidth + oHalfWidth;
}
Log3.x -= lane5s;
if (Log3.x - oHalfWidth > stage.stageWidth)
{
Log3.x = 0 - oHalfWidth;
}
else if(Log3.x + oHalfWidth < 0)
{
Log3.x = stage.stageWidth + oHalfWidth;
}
//-------------- Lane 6 -----------------------
Log4.x += lane6s;
if (Log4.x - oHalfWidth > stage.stageWidth)
{
Log4.x = 0 - oHalfWidth;
}
else if(Log4.x + oHalfWidth < 0)
{
Log4.x = stage.stageWidth + oHalfWidth;
}
Log5.x += lane6s;
if (Log5.x - oHalfWidth > stage.stageWidth)
{
Log5.x = 0 - oHalfWidth;
}
else if(Log5.x + oHalfWidth < 0)
{
Log5.x = stage.stageWidth + oHalfWidth;
}
//-------------- Lane 7 -----------------------
Log6.x -= lane7s;
if (Log6.x - oHalfWidth > stage.stageWidth)
{
Log6.x = 0 - oHalfWidth;
}
else if(Log6.x + oHalfWidth < 0)
{
Log6.x = stage.stageWidth + oHalfWidth;
}
Log7.x -= lane7s;
if (Log7.x - oHalfWidth > stage.stageWidth)
{
Log7.x = 0 - oHalfWidth;
}
else if(Log7.x + oHalfWidth < 0)
{
Log7.x = stage.stageWidth + oHalfWidth;
}
Log8.x -= lane7s;
if (Log8.x - oHalfWidth > stage.stageWidth)
{
Log8.x = 0 - oHalfWidth;
}
else if(Log8.x + oHalfWidth < 0)
{
Log8.x = stage.stageWidth + oHalfWidth;
}
//-------------- Lane 8 -----------------------
Log9.x += lane8s;
if (Log9.x - oHalfWidth > stage.stageWidth)
{
Log9.x = 0 - oHalfWidth;
}
else if(Log9.x + oHalfWidth < 0)
{
Log9.x = stage.stageWidth + oHalfWidth;
}
Log10.x += lane8s;
if (Log10.x - oHalfWidth > stage.stageWidth)
{
Log10.x = 0 - oHalfWidth;
}
else if(Log10.x + oHalfWidth < 0)
{
Log10.x = stage.stageWidth + oHalfWidth;
}
//-------------- Lane 9 -----------------------
Log11.x -= lane9s;
if (Log11.x - oHalfWidth > stage.stageWidth)
{
Log11.x = 0 - oHalfWidth;
}
else if(Log11.x + oHalfWidth < 0)
{
Log11.x = stage.stageWidth + oHalfWidth;
}
Log12.x -= lane9s;
if (Log12.x - oHalfWidth > stage.stageWidth)
{
Log12.x = 0 - oHalfWidth;
}
else if(Log12.x + oHalfWidth < 0)
{
Log12.x = stage.stageWidth + oHalfWidth;
}
Log13.x -= lane9s;
if (Log13.x - oHalfWidth > stage.stageWidth)
{
Log13.x = 0 - oHalfWidth;
}
else if(Log13.x + oHalfWidth < 0)
{
Log13.x = stage.stageWidth + oHalfWidth;
}
Log14.x -= lane9s;
if (Log14.x - oHalfWidth > stage.stageWidth)
{
Log14.x = 0 - oHalfWidth;
}
else if(Log14.x + oHalfWidth < 0)
{
Log14.x = stage.stageWidth + oHalfWidth;
}
//----------- End Lanes -------------------------//
}
function HitDie(event:Event):void
{
//------------- CAR HIT ---------------------//
if(Frog1.hitTestObject(Car1))
{
Frog1.gotoAndStop(6);
death = true;
}
if(Frog1.hitTestObject(Car2))
{
Frog1.gotoAndStop(6);
death = true;
}
if(Frog1.hitTestObject(Car3))
{
Frog1.gotoAndStop(6);
death = true;
}
if(Frog1.hitTestObject(Car4))
{
Frog1.gotoAndStop(6);
death = true;
}
if(Frog1.hitTestObject(Car5))
{
Frog1.gotoAndStop(6);
death = true;
}
if(Frog1.hitTestObject(Car6))
{
Frog1.gotoAndStop(6);
death = true;
}
//------------- TRUCK HIT ---------------------//
if(Frog1.hitTestObject(Truck1))
{
Frog1.gotoAndStop(6);
death = true;
}
if(Frog1.hitTestObject(Truck2))
{
Frog1.gotoAndStop(6);
death = true;
}
if(Frog1.hitTestObject(Truck3))
{
Frog1.gotoAndStop(6);
death = true;
}
if(Frog1.hitTestObject(Truck4))
{
Frog1.gotoAndStop(6);
death = true;
}
if(Frog1.hitTestObject(Truck5))
{
Frog1.gotoAndStop(6);
death = true;
}
}
function Revive(event:Event):void
{
if(death == true)
{
Frog1.x = 149.5;
Frog1.y = 339.75;
Frog1.gotoAndStop(1);
death = false;
}
}
}
}