我的无类 AS2 编程在精神上是否接近 OOP?

发布于 2024-12-11 02:25:01 字数 2671 浏览 0 评论 0原文

因此,我是一名 Flash 游戏开发人员,试图从 AS2 过渡到 AS3+OOP,仅比其他人晚了 3 年左右。有大量免费且非常有用的材料可供浏览,我已经花了 2-3 天的时间来研究其中的一些内容,但现在我觉得我只想开始尝试并学习更​​难的东西随着我的进展(就像我对 AS2 所做的那样)。

我得到了 OOP(大多数方面)的好处,并且我曾经被迫学习在处理中做几个游戏,这让我编写了一些类,我真的很喜欢整个继承的东西,这是我这样做的最大原因我想,渴望继续前进。

我只是想问一下游戏结构 - 我当前的 AS2 编程方式(参见下面的示例,带有一些伪代码)是否接近您在 OOP 中组织事物的方式,或者我的游戏逻辑中是否存在一些重大缺陷/我看不到的结构?我理解在 AS3/OOP 中必须做不同的事情的方式是有一个用于移动诸如玩家、英雄、导弹等东西的类,然后有一个扩展该类的敌人类,然后为每个扩展的敌人有类敌人类,与现在不同,每个“类”是一个对象和一个从主游戏循环调用的函数,而子类是每个函数中的“if”子句。但除此之外,我的编程风格是否走在正确的轨道上,或者我是否需要重新思考代码背后的逻辑才能使其在 AS3/OOP 设置中有效工作?

任何建议将不胜感激!

function initializeF() {
    initializeVariablesF();
    startGameF();
}

function initializeVariablesF() {
    enemyO = new Object(); //which will contain each enemy instance
    enemyA = new Array(); //which will be a list of all the enemies, maybe superfluous?
    playerShotsA=new Array();
    //and so on...
    enemyDataO = new Object();
    enemyDataO.goblin = new Object();
    //and then some vars relating to the goblin, sort of a class without methods, right?
}

function startGameF() {
    this.onEnterFrame = function() { //my game loop
        checkKeysF(); //checks which keys are pressed, saves it to a global object
        playerMovementF(); //moves the player about depending on which keys are pressed
        playerShotsF(); //deals with the missiles/shots/lasers the player has shot
        enemyCreatorF(); //decides when to create a new enemy
        enemyF(); //deals with all enemies in the enemyA
        enemyShotsF(); //deals with the missiles/etc the enemies have created
    };
}

function enemyCreatorF(){
    //randomly creates an enemy through a "factory" function:
    if (random(20)==1){
        attachEnemyF(enemyDataO.goblin, ...and some values like position etc)
    }
}
function attachEnemyF(a_enemyType, ... a bunch of values like position){
    //create a new enemy object
    var enemy=enemyO[new unique enemy name]
    enemy.enemyType=a_enemyType
    enemy.clip=attachMovie(a_enemyType,"clip", [values like x and y passed on])
    enemyA.push(enemy) 
}
function playerShotsF(){
    for (every shot in playerShotsA){
        //move it about
        for (every enemy in enemyA){
            //if it hits an enemy, add damage to the enemy
        }
    }
}

function enemyF() {
    for (every enemy in enemyA) {
        //check if it's dead/less health than zero, if so remove it from the array, remove it's clip, null it's object
        //if it's not, move it about, maybe have it shoot
        //if it touches the player, decrease the player's health
        //different behavior depending on the enemy's type by "if" or "switch" statements
    }
}

So I'm a flash game developer trying to make the transition from AS2 to AS3+OOP, only 3 years or so after everybody else did. There's a plethora of free, very helpful material to go through and I've been spending 2-3 days wrapping my head around some of it, but by now I feel like I just want to start and try it out and learn the harder stuff as I go (just like I did with AS2).

I get the benefits of (most aspects of) OOP and I was once forced to learn to do a couple of games in Processing which had me write a few classes and I really liked the whole inheritance thing, which is the biggest reason I'm keen to move on, I think.

I just wanted to ask about game structure - is my current way of AS2 programming (see the example below, with some pseudo code) close to the way you'd organize things in OOP, or are there some big flaws in my game logic/structure that I can't see? The way I understand I would have to do differently in AS3/OOP is to have a class for moving stuff such as the player, hero, missiles etc, then have an enemy class that extends that class, then have classes for each enemy that extends the enemy class, unlike now where each "class" is instead an object and a function called from the main game loop, and sub-classes are instead "if"-clauses in each function. But except for that, is my programming style on the right track or do I need to re-think the logic behind my code for it to work effectively in an AS3/OOP setting?

Any advice will be much appreciated!

function initializeF() {
    initializeVariablesF();
    startGameF();
}

function initializeVariablesF() {
    enemyO = new Object(); //which will contain each enemy instance
    enemyA = new Array(); //which will be a list of all the enemies, maybe superfluous?
    playerShotsA=new Array();
    //and so on...
    enemyDataO = new Object();
    enemyDataO.goblin = new Object();
    //and then some vars relating to the goblin, sort of a class without methods, right?
}

function startGameF() {
    this.onEnterFrame = function() { //my game loop
        checkKeysF(); //checks which keys are pressed, saves it to a global object
        playerMovementF(); //moves the player about depending on which keys are pressed
        playerShotsF(); //deals with the missiles/shots/lasers the player has shot
        enemyCreatorF(); //decides when to create a new enemy
        enemyF(); //deals with all enemies in the enemyA
        enemyShotsF(); //deals with the missiles/etc the enemies have created
    };
}

function enemyCreatorF(){
    //randomly creates an enemy through a "factory" function:
    if (random(20)==1){
        attachEnemyF(enemyDataO.goblin, ...and some values like position etc)
    }
}
function attachEnemyF(a_enemyType, ... a bunch of values like position){
    //create a new enemy object
    var enemy=enemyO[new unique enemy name]
    enemy.enemyType=a_enemyType
    enemy.clip=attachMovie(a_enemyType,"clip", [values like x and y passed on])
    enemyA.push(enemy) 
}
function playerShotsF(){
    for (every shot in playerShotsA){
        //move it about
        for (every enemy in enemyA){
            //if it hits an enemy, add damage to the enemy
        }
    }
}

function enemyF() {
    for (every enemy in enemyA) {
        //check if it's dead/less health than zero, if so remove it from the array, remove it's clip, null it's object
        //if it's not, move it about, maybe have it shoot
        //if it touches the player, decrease the player's health
        //different behavior depending on the enemy's type by "if" or "switch" statements
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风不识月 2024-12-18 02:25:01

我不确定这个问题是否适合 SO,因为它主要是在征求意见,但无论如何:

  1. 拥有一个具有“move”、“hitTest”、“render”等基本功能的基类是确实是你应该做的。然后让玩家的头像、敌人等继承这个类。

  2. F 后缀(甚至 O 和 A 后缀)是相当多余的,因为任何好的 AS3 编辑器都会告诉您类成员是函数、数组、对象等。

  3. 你不需要存储你的数组和对象中都有敌人,这将使删除或添加敌人变得不必要的复杂化。相反,只需将它们全部存储在一个数组中,并使用简单的循环来检查它们的属性。

I'm not sure the question is a good fit for SO as it's mostly asking for opinions, but anyway:

  1. Having a base class with basic functions such as "move", "hitTest", "render", etc. is indeed what you should do. Then have the player's avatar, enemies, etc. inherit from this class.

  2. The F suffix (and even O and A suffixes) are quite superfluous, since any good AS3 editor will already tell you whether the class member is a function, or an array, object, etc.

  3. You don't need to store your enemies in both an array and an object, it's going to make it unnecessary complicated to remove or add an enemy. Instead, just store them all in an array and use simple loops to inspect their properties.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文