Arkanoid z wolną paletką

0

Napisałam sobie arkanoida idąc za pewnym tutorialem (jest to mój pierwszy program w AS3). Mam z nim jednak taki problem: wymyśliłam sobie, że paletka ma być "wolna", znaczy, że może poruszać się we wszystkie strony, a nie tylko w prawo-lewo. Niestety, w takim trybie bardzo prosto można zauważyć niedoskonałość gry - wystarczy ruszyć paletką w górę tuż przed odbiciem, a piłka przeleci przez paletkę.
Kombinuję i kombinuję, ale wszystkie moje pomysły nic nie poprawiały tylko wprowadzały nowe problemy. Próbowałam najpierw sprawdzać kolizję dla nowego położenia piłki, i dopiero wtedy ją przemieszczać - to mi się piłeczka odbijała za wcześnie (jakby od powietrza). Próbowałam zwiększać fps, że niby zdarzenie jest na enter_frame, może za rzadko - osiągnęłam tylko tyle, że piłeczka zapitala, zachowując się tak samo. Próbowałam zamienić ze zdarzenia enter_frame na setInterval i tak wywoływać - też nie pomogło. No i skończyły mi się pomysły...

stop();

function beginCode(event:MouseEvent):void{
	//removes the listener for a click
	stage.removeEventListener(MouseEvent.CLICK, beginCode);
	
	mcPaddle.addEventListener(Event.ENTER_FRAME, movePaddle);
	mcBall.addEventListener(Event.ENTER_FRAME, moveBall);
	addEventListener(Event.ENTER_FRAME, checkLevel);
	
	mcBall.visible = true;
	startTxt.text = "";
	
	ballXSpeed = 10; //X Speed of the Ball
	ballYSpeed = 10; //Y Speed of the Ball
}

function resetBall():void{
	mcBall.y = stage.stageHeight/2 - mcBall.height;
	mcBall.x = stage.stageWidth/2 - mcBall.width;
	mcBall.visible = false;
	ballXSpeed = 0;
	ballYSpeed = 0;
	
	startTxt.text = "click to begin";
	stage.addEventListener(MouseEvent.CLICK, beginCode);
}

function movePaddle(event:Event):void
{
	if (paddleIsFree)
	{
		mcPaddle.y = stage.mouseY - mcPaddle.height / 2;

		if(mouseY < mcPaddle.height / 2)
		{
			mcPaddle.y = 0;
		}

		if(mouseY > stage.stageHeight - mcPaddle.height / 2)
		{
			mcPaddle.y = stage.stageHeight - mcPaddle.height;
		}
	}
	//The paddle follows the mouse
	mcPaddle.x = stage.mouseX - mcPaddle.width / 2;
	
	//If the mouse goes off too far to the left
	if(mouseX < mcPaddle.width / 2)
	{
		//Keep the paddle on stage
		mcPaddle.x = 0;
	}
	//If the mouse goes off too far to the right
	if(mouseX > stage.stageWidth - mcPaddle.width / 2)
	{
		//Keep the paddle on stage
		mcPaddle.x = stage.stageWidth - mcPaddle.width;
	}
}
 
function moveBall(event:Event):void
{
	mcBall.x += ballXSpeed; //* Math.sin (45 * Math.PI / 180) ; 
	mcBall.y += ballYSpeed; //* Math.sin (45 * Math.PI / 180) ; 

	//Bouncing the ball off of the walls
	if(mcBall.x >= stage.stageWidth-mcBall.width-1){
		//if the ball hits the right side
		//of the screen, then bounce off
		ballXSpeed *= -1;
	}
	if(mcBall.x <= 1){
		//if the ball hits the left side
		//of the screen, then bounce off
		ballXSpeed *= -1;
	}
	if(mcBall.y >= stage.stageHeight-mcBall.height-1){
		//if the ball hits the bottom
		//then bounce up
		ballYSpeed *= -1;
	}
	if(mcBall.y <= 1){
		//if the ball hits the top
		//then bounce down
		ballYSpeed *= -1;
	}
	
	if (mcBall.hitTestObject(mcPaddle)) {
		mcAlert.visible = ! mcAlert.visible;
		calcBallAngle();
	}


	if(mcBall.y >= stage.stageHeight-mcBall.height){
		//if the ball hits the bottom
		//then bounce up and lose a life
		ballYSpeed *= -1;
		lives --;

		resetBall();
		//if there aren't any lives left
		if(lives <= 0){
			//the game is over now
			gameOver = true;

			//go to a lose frame
			gotoAndStop('lose');
		}
	}
}
function calcBallAngle():void{
	//ballPosition is the position of the ball is on the paddle
	var ballPosition:Number = mcBall.x - mcPaddle.x;
	//hitPercent converts ballPosition into a percent
	var hitPercent:Number = (ballPosition / (mcPaddle.width - mcBall.width)) - .5;
	//Gets the hitPercent and makes it a larger number so the
	//ball actually bounces
	ballXSpeed = hitPercent * 10;
	//Making the ball bounce back up
	ballYSpeed *= -1;
}


function makeLvl():void{ //Places bricks onto Level
	resetBall();
	if (currentLvl > lvlArray.length)
	{
		currentLvl = 1;
	}
	switch (currentLvl)
	{
		case 1 : 
			paddleIsFree = true;
			break;
		case 2 : 
			paddleIsFree = true;
			break;
	}
	var arrayLength:int = lvlArray[currentLvl-1].length;
	
	var brickRow:int = 0;
	//Now, creating a loop which places the bricks onto the stage
	for(var i:int = 0;i<arrayLength;i++){
		//checking if it should place a brick there
		if(lvlArray[currentLvl-1][i] == 1){
			var brick:Klocek = new Klocek();
			//setting the brick's coordinates via the i variable and brickRow
			brick.x = 15+(i-brickRow*7)*75;
			brick.y = 10+brickRow*25;
			//checks if the current brick needs a new row
			for(var c:int = 1;c<=10;c++){
				if(i == c*7-1){
					brickRow ++;
				}
			}
			//finally, add the brick to stage
			addChild(brick);
		}
	}
}

function checkLevel(event:Event):void{
	//checking if the bricks are all gone
	if(brickAmt == 0){
		//reset the level by increasing the level
		currentLvl ++;
		//and re-running makeLvl
		makeLvl();

		//then removing all of the listeners
		mcPaddle.removeEventListener(Event.ENTER_FRAME, movePaddle);
		mcBall.removeEventListener(Event.ENTER_FRAME, moveBall);
		removeEventListener(Event.ENTER_FRAME, checkLevel);
		//then listening for a mouse click to start the game again
		stage.addEventListener(MouseEvent.CLICK, beginCode);
	}
}

var ballXSpeed:Number = 0; //X Speed of the Ball
var ballYSpeed:Number = 0;
var lives:int = 3;
var brickAmt:int = 0;
var gameOver:Boolean = false;
var paddleIsFree:Boolean = false;
var collision:Boolean = false;

resetBall();
makeLvl();
0

Przepraszam, nie chce mi się kodu analizować, więc tylko koncepcyjnie odpowiem. Synchronizacja ruchów paletki i piłki. Kiedy modyfikowane jest położenie jednego, nie możesz modyfikować położenia drugiego.

1 użytkowników online, w tym zalogowanych: 0, gości: 1