1
votes

Edit: I have now included a Player.as and a addchild

I've been trying to understand how to do this all day and again learned a lot in doing so. But I've come to a point that i need help.

I know I have to do this: create a Collisions var in the Back1 class.

Because the background called Back1 is the movieclip that contains the Collisions image

I found a good site or 2 that does a good job of explaining variables and classes but i still don't get how i should solve this problem

Research after variables and classes:

http://www.republicofcode.com/tutorials/flash/as3variables/

http://www.photonstorm.com/archives/1136/flash-game-dev-tip-1-creating-a-cross-game-communications-structure

the above problem results in the folowing error but i believe it is caused by not creating a Collisions var in the Back1 class

ArgumentError: Error #1063: Argument count mismatch on Bumper(). expected: 2, value 0.

  • at flash.display::MovieClip/gotoAndStop() at DocumentClass/onRequestStart()DocumentClass.as:64] at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at MenuScreen/onClickStart()MenuScreen.as:18]

    package 
     {
    import flash.display.MovieClip;
    import flash.events.*;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    import flash.geom.Point;
    import Bumper;
    //import Back1;
    
    public class Test extends MovieClip
    {
    
        public var leftBumping:Boolean = false;
    public var rightBumping:Boolean = false;
    public var upBumping:Boolean = false;
    public var downBumping:Boolean = false;
    
    public var leftBumpPoint:Point = new Point(-30,-55);
    public var rightBumpPoint:Point = new Point(30,-55);
    public var upBumpPoint:Point = new Point(0,-120);
    public var downBumpPoint:Point = new Point(0,0);
    
    public var scrollX:Number = 0;
    public var scrollY:Number = 500;
    
    public var xSpeed:Number = 0;
    public var ySpeed:Number = 0;
    
    public var speedConstant:Number = 4;
    public var frictionConstant:Number = 0.9;
    public var gravityConstant:Number = 1.8;
    public var jumpConstant:Number = -35;
    public var maxSpeedConstant:Number = 18;
    
    public var doubleJumpReady:Boolean = false;
    public var upReleasedInAir:Boolean = false;
    
    public var keyCollected:Boolean = false;
    public var doorOpen:Boolean = false;
    
    public var currentLevel:int = 1;
    
    public var animationState:String = "idle";
    
    public var bulletList:Array = new Array();
    public var enemyList:Array = new Array();
    public var bumperList:Array = new Array();
     public var back1:Back1;
    public var collisions:Collisions;
    //public var back1:Collisions = new Collisions ;
     public var player:Player;
    
    public function Test()
    {
        addEventListener(Event.ADDED_TO_STAGE, init);
    }
    
    public function init(e:Event):void
    {
    
        player = new Player(320, 360);
        back1 = new Back1();
        collisions = new Collisions();
        //back1.collisions = new Collisons();
              addBumpersToLevel1();
    }
    public function addBumpersToLevel1():void
    {
        addBumper(500, -115);
        addBumper(740, -115);
    }
            public function addPlayerTolevel1():void 
    {
        addPlayer(320, 360);
    }
    public function loop(e:Event):void
    {
        trace("back1.collisions "+back1.collisions);
                    trace("back1 "+back1);
                    trace("collisions "+collisions);
        if (back1.collisions.hitTestPoint(player.x + leftBumpPoint.x,player.y + leftBumpPoint.y,true))
        {
    

just in case i've added Bumper.as

package  {
import flash.display.MovieClip;
import flash.events.Event;

public class Bumper extends MovieClip{
    public function Bumper(xLocation:int, yLocation:int) {
        // constructor code
        x = xLocation;
        y = yLocation;

        addEventListener(Event.ENTER_FRAME, bumper);
    }

    public function bumper(e:Event):void{
        //code here
    }
}

}

Player.as

 package  {
    import flash.display.MovieClip;
    import flash.events.Event;

    public class Player extends MovieClip {

        public function Player(xLocation:int, yLocation:int) {
            // constructor code
            x = xLocation;
            y = yLocation;
           }



     //   public function removeSelf():void {
      //      trace("remove enemy");
       //     removeEventListener(Event.ENTER_FRAME, loop);
       //     this.parent.removeChild(this);
       // }



 }
 }

the Back1.as file (note it's got to be instanced wrong)

package  {
import flash.display.MovieClip;


public class Back1 extends MovieClip {
    //public var collisions:Back1;
           //what should i put here?
}
}
2
Error said you passed zero arguments to constructor of Bumper class. How do you create it?Serge Him
i've added some public var that i had cut out to make it more transparant....for example....public var bumperList:Array = new Array();Jacques
i think it now has the missing code for bumperJacques
It sounds like you might have a "Bumper" movie clip on the timeline, which is causing the public function Bumper() to run without an xLocation and yLocation.David Mear
no it's not on the timeline it's created with addchild. One of the problems was just solved by Serge but it still gives a problem for the line if (back1.collisions.hitTestPoint(player.x + leftBumpPoint.x,player.y + leftBumpPoint.y,true)) the erorr it returns is a nullJacques

2 Answers

1
votes

I am not sure I understand completely what you mean. The question is phrased strange.

I assume you want to achieve a collision between your background object (The Back class) and a player object? I can't see from the code you have posted what the player object is since there is no such variable in your Test class.

To test a collision check between two objects use the following code:

if(someObject.hitTestObject(anotherObject))

Or in your case when using hitTestPoint:

if(back1.hitTestPoint(player.x, player.y,true))

Then again I don't know from the code you have posted how the back1 class looks like. If it extends a MovieClip or Sprite and you have a Player class that does the same (OR any DisplayObject) this should work.

This: Argument count mismatch on Bumper(). expected: 2, value 0.

The error you get seems to come from another place not shown in your code. I would assume you did not pass any parameters into the Bumper class' constructor.

Btw, is this a Flash IDE sample or some other program such as FlashDevelop or FlashBuilder? If you are using the Flash IDE and are trying to attach code to a movie clip instance placed out on the scene I don't think its possible to pass parameters to it. Sorry been a while since I've worked in the Flash IDE.

EDIT:

Here's some sample code:

//::  Change Back1 class to this

package  {
import flash.display.MovieClip;


public class Back1 extends MovieClip {
    public function Back1()
    {
        graphics.beginFill(0xFF0000);
        graphics.drawRect(0, 0, 50, 50);
        graphics.endFill();
    }
}
}


//::  Then in your Main class (Or the Test class) add the following

var player:Player = new Player(25, 25);
var collidable:Back1 = new Back1();

addChild(player);
addChild(collidable);

//::  Goes in your loop/update
if (collidable.hitTestPoint(player.x, player.y, true))
{
    trace("HIT PLAYER");
}

How you apply the graphics to the Back1 class is up to you, I just drew a simple box. It could be anything.

0
votes

Set default parameters for Bumper class:

package  {
    import flash.display.MovieClip;
    import flash.events.Event;

    public class Bumper extends MovieClip{
        public function Bumper(xLocation:int = 0, yLocation:int = 0) {
            // constructor code
            x = xLocation;
            y = yLocation;

            addEventListener(Event.ENTER_FRAME, bumper);
        }

        public function bumper(e:Event):void{
            //code here
        }
    }

    }