AS3 platform AI modularization issue - help please
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: AS3 platform AI modularization issue - help please

  1. #1
    Junior Member
    Join Date
    Mar 2008
    Posts
    2

    AS3 platform AI modularization issue - help please

    Guys,

    I have a problem about the flash platform game I'm developing. I want to modularize my code but somethings not right. Here's the scenario:

    I have 2 movieclips that represent the enemies in my platform game.
    the instance names of these movieclips are:

    enemy_mc1
    enemy_mc2

    The code below works perfectly fine with enemy_mc1
    but in enemy_mc2 it doesn't but if I place the enemyMode(enemy_mc2) on top of enemyMode(enemy_mc1). Now, enemy_mc2 works and enemy_mc1 doesn't.

    addEventListener(Event.ENTER_FRAME,enemyMovement);
    function enemyMovement(event:Event):void {
    enemyMode(enemy_mc1);
    enemyMode(enemy_mc2);

    }


    function enemyMode(enemy_mc:MovieClip):void {

    var enemy_timeDiff:int = getTimer()-enemy_lastTime;
    enemy_lastTime += enemy_timeDiff;
    // adjust vertical speed for gravity
    enemy_char_mc_dy += enemy_gravity*enemy_timeDiff;
    var enemy_verticalChange:Number = enemy_char_mc_dy*enemy_timeDiff + enemy_timeDiff*enemy_char_mc_dy;
    var enemy_HorizontalChange = 0;
    if (enemy_verticalChange > 15.0) {
    enemy_verticalChange = 15.0;
    }
    for (var a:Number = 1; a<3; a++) {
    if (char_mc.x<enemy_mc.x) {
    if ((enemy_mc.x-char_mc.x) <=20) {
    enemy_mc.x -= 0;
    } else if ((enemy_mc.x-char_mc.x) >=20) {
    enemy_HorizontalChange = -enemy_char_mc_walkSpeed*enemy_timeDiff;
    }
    if ((enemy_mc.hitTestObject(this["point_mc"+a]))&& (char_mc.y < enemy_mc.y)) {
    enemy_char_mc_dy = -enemy_char_mc_jumpSpeed;
    enemy_verticalChange = -enemy_char_mc_jumpSpeed;

    }
    } else if (char_mc.x>enemy_mc.x) {
    if ((enemy_mc.x-char_mc.x) >=-20) {
    enemy_mc.x += 0;
    }
    if ((enemy_mc.x-char_mc.x) <=-20) {
    enemy_HorizontalChange = +enemy_char_mc_walkSpeed*enemy_timeDiff;
    }
    if ((enemy_mc.hitTestObject(this["point_mc"+a]))&& (char_mc.y < enemy_mc.y)) {
    enemy_char_mc_dy = -enemy_char_mc_jumpSpeed;
    enemy_verticalChange = -enemy_char_mc_jumpSpeed;
    }
    }
    }
    var enemyNewY:Number = enemy_mc.y + enemy_verticalChange;
    //loop through all fixed objects to see if character has landed
    for (var k:Number=1; k<numOfFixedObejcts; k++) {
    if ((enemy_mc.x+enemy_mc.width/2 > (this["fixedObject_mc"+k].x - (this["fixedObject_mc"+k].width/2))) && (enemy_mc.x-enemy_mc.width/2 <(this["fixedObject_mc"+k].x+ (this["fixedObject_mc"+k].width/2)))) {
    if ((enemy_mc.y <= this["fixedObject_mc"+k].y) && (enemyNewY > this["fixedObject_mc"+k].y)) {
    enemyNewY = this["fixedObject_mc"+k].y;

    enemy_char_mc_dy = 0;
    enemy_char_mc_inAir = false;

    }
    }
    }
    // find new horizontal position
    var enemyNewX:Number = enemy_mc.x + enemy_HorizontalChange;
    for (var j:Number=1; j<numOfFixedObejcts; j++) {
    if ((enemyNewY > this["fixedObject_mc"+j].y) && (enemyNewY-enemy_mc.height < (this["fixedObject_mc"+j].y+this["fixedObject_mc"+j].height))) {
    if ((enemy_mc.x-enemy_mc.width/2 >= (this["fixedObject_mc"+j].x+this["fixedObject_mc"+j].width/2)) && (enemyNewX-enemy_mc.width/2 <= (this["fixedObject_mc"+j].x+this["fixedObject_mc"+j].width/2))) {
    enemyNewX = (this["fixedObject_mc"+j].x+this["fixedObject_mc"+j].width/2)+enemy_mc.width/2;
    }
    if ((enemy_mc.x+enemy_mc.width/2 <= (this["fixedObject_mc"+j].x-this["fixedObject_mc"+j].width/2)) && (enemyNewX+enemy_mc.width/2 >= (this["fixedObject_mc"+j].x-this["fixedObject_mc"+j].width/2))) {
    enemyNewX = (this["fixedObject_mc"+j].x-this["fixedObject_mc"+j].width/2)-enemy_mc.width/2;
    }
    }
    }
    enemy_mc.y = enemyNewY;
    enemy_mc.x = enemyNewX;

    }

    please please help me peeps
    Any help will be appreciated

  2. #2
    Junior Member
    Join Date
    Dec 2013
    Location
    Indiana
    Posts
    5
    And the problem is......?

  3. #3
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    946
    Try this:

    Replace these lines at the beginning
    addEventListener(Event.ENTER_FRAME,enemyMovement);
    function enemyMovement(event:Event):void {
    enemyMode(enemy_mc1);
    enemyMode(enemy_mc2);

    }
    with:


    enemy_mc1.addEventListener(Event.ENTER_FRAME, enemyMovement);
    enemy_mc2.addEventListener(Event.ENTER_FRAME, enemyMovement);
    function enemyMovement(event:Event):void {
    enemyMode(event.currentTarget);
    }
    Hi Object, Listen, i wish to return you something, but only if what you are saying is true, else, forget it, ROLL_OVER the floor, i will check for someone else at the stage, while eating a no.visible cookie.

  4. #4
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    946
    I don't know if this has something to do with, but there's a typo in the script:

    numOfFixedObejcts should be numOfFixedObjects;
    Hi Object, Listen, i wish to return you something, but only if what you are saying is true, else, forget it, ROLL_OVER the floor, i will check for someone else at the stage, while eating a no.visible cookie.

  5. #5
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    946
    I don't know if this solve your issue, as you didn't make any reference to the variables, i had to guess them (a shot in the dark) and build the movieclips according to the script to test your script.

    With my edited script, but movieclips moves, just add the correct variables values:

    enemy_mc2.addEventListener(Event.ENTER_FRAME, enemyMovement);
    enemy_mc1.addEventListener(Event.ENTER_FRAME, enemyMovement);

    function enemyMovement(event:Event):void {
    enemyMode(MovieClip(event.currentTarget));
    }

    function enemyMode(enemy_mc:MovieClip):void {
    //I had to add this variables so the .fla won't show any referencing error
    var enemy_char_mc_dy = 10;
    var enemy_gravity=0.2;
    var enemy_char_mc_walkSpeed = .5;
    var enemy_char_mc_jumpSpeed =50;
    var enemy_lastTime = 20;
    var numOfFixedObejcts:int = 5;
    var enemy_char_mc_inAir;

    var enemy_timeDiff:int = getTimer()-enemy_lastTime;
    enemy_lastTime += enemy_timeDiff;
    // adjust vertical speed for gravity
    enemy_char_mc_dy += enemy_gravity*enemy_timeDiff;
    var enemy_verticalChange:Number = enemy_char_mc_dy*enemy_timeDiff + enemy_timeDiff*enemy_char_mc_dy;
    var enemy_HorizontalChange = 0;
    if (enemy_verticalChange > 15.0) {
    enemy_verticalChange = 15.0;
    }
    for (var a:Number = 1; a<3; a++) {
    if (char_mc.x<enemy_mc.x) {
    if ((enemy_mc.x-char_mc.x) <=20) {
    enemy_mc.x -= 0;
    } else if ((enemy_mc.x-char_mc.x) >=20) {
    enemy_HorizontalChange = -enemy_char_mc_walkSpeed*enemy_timeDiff;
    }
    if ((enemy_mc.hitTestObject(this["point_mc"+a]))&& (char_mc.y < enemy_mc.y)) {
    enemy_char_mc_dy = -enemy_char_mc_jumpSpeed;
    enemy_verticalChange = -enemy_char_mc_jumpSpeed;

    }
    } else if (char_mc.x>enemy_mc.x) {
    if ((enemy_mc.x-char_mc.x) >=-20) {
    enemy_mc.x += 0;
    }
    if ((enemy_mc.x-char_mc.x) <=-20) {
    enemy_HorizontalChange = +enemy_char_mc_walkSpeed*enemy_timeDiff;
    }
    if ((enemy_mc.hitTestObject(this["point_mc"+a]))&& (char_mc.y < enemy_mc.y)) {
    enemy_char_mc_dy = -enemy_char_mc_jumpSpeed;
    enemy_verticalChange = -enemy_char_mc_jumpSpeed;
    }
    }
    }
    var enemyNewY:Number = enemy_mc.y + enemy_verticalChange;
    //loop through all fixed objects to see if character has landed
    for (var k:Number=1; k<numOfFixedObejcts; k++) {
    if ((enemy_mc.x+enemy_mc.width/2 > (this["fixedObject_mc"+k].x - (this["fixedObject_mc"+k].width/2))) && (enemy_mc.x-enemy_mc.width/2 <(this["fixedObject_mc"+k].x+ (this["fixedObject_mc"+k].width/2)))) {
    if ((enemy_mc.y <= this["fixedObject_mc"+k].y) && (enemyNewY > this["fixedObject_mc"+k].y)) {
    enemyNewY = this["fixedObject_mc"+k].y;

    enemy_char_mc_dy = 0;
    enemy_char_mc_inAir = false;

    }
    }
    }
    // find new horizontal position
    var enemyNewX:Number = enemy_mc.x + enemy_HorizontalChange;
    for (var j:Number=1; j<numOfFixedObejcts; j++) {
    if ((enemyNewY > this["fixedObject_mc"+j].y) && (enemyNewY-enemy_mc.height < (this["fixedObject_mc"+j].y+this["fixedObject_mc"+j].height))) {
    if ((enemy_mc.x-enemy_mc.width/2 >= (this["fixedObject_mc"+j].x+this["fixedObject_mc"+j].width/2)) && (enemyNewX-enemy_mc.width/2 <= (this["fixedObject_mc"+j].x+this["fixedObject_mc"+j].width/2))) {
    enemyNewX = (this["fixedObject_mc"+j].x+this["fixedObject_mc"+j].width/2)+enemy_mc.width/2;
    }
    if ((enemy_mc.x+enemy_mc.width/2 <= (this["fixedObject_mc"+j].x-this["fixedObject_mc"+j].width/2)) && (enemyNewX+enemy_mc.width/2 >= (this["fixedObject_mc"+j].x-this["fixedObject_mc"+j].width/2))) {
    enemyNewX = (this["fixedObject_mc"+j].x-this["fixedObject_mc"+j].width/2)-enemy_mc.width/2;
    }
    }
    }
    enemy_mc.y = enemyNewY;
    enemy_mc.x = enemyNewX;
    }
    Hi Object, Listen, i wish to return you something, but only if what you are saying is true, else, forget it, ROLL_OVER the floor, i will check for someone else at the stage, while eating a no.visible cookie.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width


HTML5 Development Center

Poll by Flashkit.com