gpt4 book ai didi

actionscript-3 - ActionScript 3 : How do I access the properties of the document class?

转载 作者:行者123 更新时间:2023-12-02 07:54:24 25 4
gpt4 key购买 nike

当我尝试从另一个类访问文档类的一些私有(private)属性时,它会输出以下错误:

1119: Access of possibly undefined property _player through a reference with static type flash.display:Stage.

这是文档类的代码:
    package 
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.ui.Keyboard;
import collision;
import player;
import ground;

public class engine extends MovieClip
{
public var _player:player;
private var _ground:ground;
private var _collision:collision;
private var _right:Boolean;
private var _space:Boolean;
private var _left:Boolean;
private var _touchGround:Boolean;
private var _jump:Boolean;
private var _jumpVel:int;
private var _q:int;
private var _vx:int;
private var _vy:int;

public function engine()
{
_player = new player();
_ground = new ground();
_collision = new collision();
addChild(_player);
addChild(_ground);
_player.x = stage.stageWidth/2 - _player.width/2;
_player.y = stage.stageHeight/2 - _player.height/2;
_ground.x = stage.stageWidth/2 - _ground.width/2;
_ground.y = stage.stageHeight/2 - _ground.height/2;
_ground.y += 150;
_ground.x += 300;
_q = 0;
stage.addEventListener(Event.ENTER_FRAME, enterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
}
private function enterFrame(e:Event)
{
if(_right)
{
if(_vx > 15)
{
_vx = 15;
}
_vx += 2;
}
if(_left)
{
if(_vx < -15)
{
_vx = -15;
}
_vx -= 2;
}
if(_space && _touchGround)
{
_jump = true;
}
if(_jump)
{
_jumpVel = 20 - _q;
if(_q == 20)
{
_q = 0;
_jumpVel = 0;
_jump = false;
}
else
{
_ground.y += _jumpVel;
_q ++;
}
}
_collision.detectCollisions();
_ground.x -= _vx;
_ground.y += _vy;
if(_vx > 0)
{
_vx--;
if(_vx < 0)
{
_vx = 0;
}
}
else if(_vx < 0)
{
_vx++;
if(_vx > 0)
{
_vx = 0;
}
}
if(_vy > 0)
{
_vy = 0;
}
else if(_vy < -10)
{
_vy = -10;
}
trace(_vy);
}

private function keyDownHandler(e:KeyboardEvent)
{
if(e.keyCode == Keyboard.RIGHT)
{
_right = true;
}
if(e.keyCode == Keyboard.LEFT)
{
_left = true;
}
if(e.keyCode == Keyboard.SPACE)
{
_space = true;
}
}
private function keyUpHandler(e:KeyboardEvent)
{
if(e.keyCode == Keyboard.RIGHT)
{
_right = false;
}
if(e.keyCode == Keyboard.LEFT)
{
_left = false;
}
if(e.keyCode == Keyboard.SPACE)
{
_space = false;
}

}
}
}

这是“碰撞”类的代码。
package
{

import flash.display.MovieClip;
import player;
import engine;

public class collision extends MovieClip
{

private var _playerCol:player = engine._player;


public function collision()
{
}
public function detectCollisions():void
{
_playerCol.y += 7;
}
}
}

我正在尝试从碰撞类访问属性“_player”,但出现错误。

最佳答案

目前,您正尝试访问 _player,就好像它是 Engine 类的静态成员一样。您可以将 _player 设为静态,但这会导致代码异味。一种安全的方法是将引用传递给构造函数:

private var _engine:Engine
private var _playerCol:player;
public function collision(engine:Engine){

_engine = engine;
_playerCol = _engine._player
}

所以在你的 Doc 课上你会有这条线
_collision = new collision(this);

PS。作为提示,类在开始时确实应该有一个大写字母,如果一个变量要公开,请删除下划线,否则将变量保留为私有(private)并为其创建公共(public)属性。此外,您可能需要考虑更改结构,因为它有些笨拙。我建议查看组件实体系统。

关于actionscript-3 - ActionScript 3 : How do I access the properties of the document class?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1735478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com