Different AS3 code for each frame

Need to create different level with different "rules"

Page 1 of 1

4 Replies - 3701 Views - Last Post: 28 September 2009 - 11:06 AM

#1 ProgrammerZach  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 12
  • Joined: 23-July 09

Different AS3 code for each frame

Post icon  Posted 27 September 2009 - 10:28 AM

So, I fixed my issue with getting it to go to a new level, but now I have run into another issue. Since I have specific code for things such as my doorknob, chimney, etc. when it goes to the new level where there is no knob, chimney, etc. It goes crazy with errors. I think I have to do something with a package, but im not sure. So basically I want my AS3 layer to corespond with 1 level, not have 1 layer set the rules for all my levels.
Is This A Good Question/Topic? 0
  • +

Replies To: Different AS3 code for each frame

#2 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: Different AS3 code for each frame

Posted 28 September 2009 - 02:50 AM

Presumably your errors are to do with the function that is called by your ENTER_FRAME event? In the previous frame, when you decide to move to frame 2 you should clear all the event listeners that you are using. If you don't clear the listener then the function from frame 1 will continue to be called, and errors will occur because your doorknob and chimny are no longer present on the stage.

function collide(event:Event) {
	if (hero.hitTestObject(knob)) {
		errormsg.text="The Door Is Locked, Try a Different Tactic.";
	} else {
		errormsg.text="Welcome to your Birthday Game Dad!";
	}
	if (hero.hitTestObject(chimney)) {
		//remove event listener
		this.removeEventListener(Event.ENTER_FRAME,collide);
		gotoAndPlay(2)
	}else{
		stop();
	}
   
}


This post has been edited by thehat: 28 September 2009 - 02:52 AM

Was This Post Helpful? 0
  • +
  • -

#3 ProgrammerZach  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 12
  • Joined: 23-July 09

Re: Different AS3 code for each frame

Posted 28 September 2009 - 09:23 AM

Thanks for the reply! I tried it, but it doesnt seem to help.

Im geting the error message
"TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at dadlevel1draft3_fla::MainTimeline/collide()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()" over and over

I might have done something wrong? My code for frame 1 (level 1) is:

var vy:Number=0;
var jumped:Boolean=false;
var gv:Number=1;


addEventListener(Event.ENTER_FRAME,ctrl_hero);
stage.addEventListener(MouseEvent.CLICK, jump);
stage.addEventListener(Event.ENTER_FRAME, collide);

function ctrl_hero(e:Event) {
	if (hero.x-20<mouseX) {
		hero.x+=5;
	}
	if (hero.x+20>mouseX) {
		hero.x-=5;
	}
	vy+=gv;
	if (! level.hitTestPoint(hero.x,hero.y,true)) {
		hero.y+=vy;
	}
	if (vy>10) {
		vy=10;
	}
	for (var i:int = 0; i<10; i++) {
		if (level.hitTestPoint(hero.x,hero.y,true)) {
			hero.y--;
			vy=0;
			jumped=false;
		}
	}
}


function jump(e:Event) {
	if (! jumped) {
		hero.y-=5;
		vy=-18;
		jumped=true;
	}
}


function collide(event:Event) {
	if (hero.hitTestObject(knob)) {
		errormsg.text="The Door Is Locked, Find a different way in.";
	} else {
		errormsg.text="";
	}
	if (hero.hitTestObject(chimney)) {
		//remove event listener
		this.removeEventListener(Event.ENTER_FRAME,collide);
		gotoAndPlay(2)
	}else{
		stop();
	}
   
}







for frame 2 (level 2):

stage.addEventListener(Event.ENTER_FRAME, paperman);


function paperman(g:Event) {
	if (hero.hitTestObject(paper)) {
		errormsg.text="The Letter Reads: Dear Greg, Sorry but we forgot your birthday was today. Went to go and get you something nice, be back soon. Love, Family. PS: DONT GO IN THE KITCHEN!";
	}else{
		errormsg.text="";
	}
	if (hero.hitTestObject(knob)) {
		stop();
	}else{
		stop();
	}
}


The stops are in there until i make the next door lead somewhere.

This post has been edited by ProgrammerZach: 28 September 2009 - 09:25 AM

Was This Post Helpful? 0
  • +
  • -

#4 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: Different AS3 code for each frame

Posted 28 September 2009 - 10:05 AM

In your collide function on frame 1 the line
this.removeEventListener(Event.ENTER_FRAME,collide);

needs to be replaced with
stage.removeEventListener(Event.ENTER_FRAME,collide);

Was This Post Helpful? 1
  • +
  • -

#5 ProgrammerZach  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 12
  • Joined: 23-July 09

Re: Different AS3 code for each frame

Posted 28 September 2009 - 11:06 AM

thank you so much for helping me! it runs perfectly now, i really appreciate it :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1