2 Replies - 4723 Views - Last Post: 16 August 2011 - 12:37 PM Rate Topic: -----

#1 Jacic  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 15-June 10

New objects created at previous object's y position - AS3

Posted 15 August 2011 - 09:09 PM

In my main class I have this function being called every frame:
public function playGame():void
		{
			var newStew:Stew;
			var chanceStew:Number = Math.random() * 100;
			if(chanceStew >= 99)
			{
				newStew = new Stew();
				addChild(newStew);
			}
		}

My Stew constructor function looks like:
public function Stew()
		{
			this.x = Math.random() * (gameclass.SCREEN_WIDTH - 40);
			this.y = -20;
			speed = Math.ceil(Math.random() * 30) + 30;
			addEventListener(Event.ENTER_FRAME, moveStew);
			lastTime = getTimer();
		}

What this is supposed to do is create a Stew object above the screen, with a random x position.

This function moves the stew downward:
public function moveStew(e:Event):void
		{
			var timePassed:int = getTimer() - lastTime;
			lastTime += timePassed;
			this.y = speed * lastTime / 1000;
			if(this.y > gameclass.SCREEN_HEIGHT)
			{
				parent.removeChild(this);
				removeEventListener(Event.ENTER_FRAME, moveStew);
			}
		}

What happens is that a Stew object is created, and moves downward. Any other Stew objects are created at the y position of the previously created Stew object. Eventually, they are being created below the screen. Can someone help me figure out why this is happening?

Is This A Good Question/Topic? 0
  • +

Replies To: New objects created at previous object's y position - AS3

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: New objects created at previous object's y position - AS3

Posted 16 August 2011 - 07:06 AM

The new objects are probably referencing the new position of the first object when created. Make sure that you explicitly initialize the positions of and new object created.
Was This Post Helpful? 0
  • +
  • -

#3 Jacic  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 15-June 10

Re: New objects created at previous object's y position - AS3

Posted 16 August 2011 - 12:37 PM

I have made sure to explicitly declare the y position multiple times throughout the code, to try and figure out where it went wrong. By tracing the y pos, I know that it thinks the objects are being created at -20, but they appear much lower.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1