artur1995g |
30.06.2013 18:24 |
Как получить доступ к объекту из другого класса?
Здравствуйте!
Пишу что-то наподобие Arkanoid'а и вот столкнулся с такой проблемой:
Main.as:
Код AS3:
package
{
import flash.display.MovieClip;
public class Main extends MovieClip
{
public var paddle:Paddle=new Paddle();
public var ball:Ball=new Ball();
public function Main()
{
paddle.x = 253;
paddle.y = 765;
addChild(paddle);
ball.x = 284;
ball.y = 720;
addChild(ball);
}
}
}
Paddle.as:
Код AS3:
package
{
import flash.display.MovieClip;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Paddle extends MovieClip
{
private var left:Boolean = false;
private var right:Boolean = false;
private var speed:Number = 0;
private var speedMax:Number = 12;
private var speedMaxReverse:Number = -12;
private var speedAcceleration:Number = .25;
private var speedDeceleration:Number = .1;
private var groundFriction:Number = .95;
private var velocityX:Number = 0;
public function Paddle()
{
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
init();
}
private function init():void
{
stage.addEventListener(Event.ENTER_FRAME, runGame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, myOnPress);
stage.addEventListener(KeyboardEvent.KEY_UP, myOnRelease);
}
private function runGame(event:Event):void
{
if (right)
{
if (this.x + this.width < stage.stageWidth - 11)
{
//check if below speedMax
if (speed < speedMax)
{
//speed up
speed += speedAcceleration;
//check if above speedMax
if (speed > speedMax)
{
//reset to speedMax
speed = speedMax;
}
}
}
else
{
speed = 0;
}
}
if (left)
{
if (this.x > 11)
{
//check if below speedMaxReverse
if (speed > speedMaxReverse)
{
//speed up (in reverse)
speed -= speedAcceleration;
//check if above speedMaxReverse
if (speed < speedMaxReverse)
{
//reset to speedMaxReverse
speed = speedMaxReverse;
}
}
}
else
{
speed = 0;
}
}
speed *= groundFriction;
// calculate velocity based on speed
velocityX = 1 * speed;
// update position
this.x += velocityX;
}
private function myOnPress(event:KeyboardEvent):void
{
switch ( event.keyCode )
{
case Keyboard.LEFT :
left = true;
break;
case Keyboard.RIGHT :
right = true;
break;
}
event.updateAfterEvent();
}
private function myOnRelease(event:KeyboardEvent):void
{
switch ( event.keyCode )
{
case Keyboard.LEFT :
left = false;
break;
case Keyboard.RIGHT :
right = false;
break;
}
}
}
}
Ball.as:
Код AS3:
.....
........
if (this.hitTestObject(Main.paddle)) // 1061: Обращение к возможно неопределенному методу getChildAt через ссылку статического типа Class.
{
calcBallAngle();
}
}
function calcBallAngle():void
{
//ballPosition is the position of the ball is on the paddle
var ballPosition:Number = this.x - Main.paddle.x; // 1061: Обращение к возможно неопределенному методу getChildAt через ссылку статического типа Class.
//hitPercent converts ballPosition into a percent
//All the way to the left is -.5
//All the way to the right is .5
//The center is 0
var hitPercent:Number = (ballPosition / (Main.paddle.width - this.width)) - .5; // 1061: Обращение к возможно неопределенному методу getChildAt через ссылку статического типа Class.
//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;
}
}
На сцену добавляются ball и paddle посредством кода. Нужно проверить столкновение между ball и paddle, записать это внутри класса Ball. Ума не приложу, как сослаться на paddle внутри класса Ball. В файле Ball.as я написал ошибки компилятора напротив тех строк, которые эти ошибки вызывают.
Спасибо.
|