Skip to main content
Participant
August 8, 2015
Answered

Can't change child.x

  • August 8, 2015
  • 2 replies
  • 294 views

Creating a child creates it at the x and y of the parent platform. When I try to change the onion.x it does not return an error but it also does not change the x.

The child class.

package {

  import flash.display.MovieClip;

  import flash.events.Event;

  import com.Platform

  public class Onion extends MovieClip {

  var onionSpeed;

  public function Onion(onionSpeed, pWidth) {

  this.addEventListener(Event.ENTER_FRAME, loop);

  this.x += pWidth;

  }

  function loop(e: Event): void {

  this.x -= onionSpeed;

}

}

}

The parent class.

package {

  import flash.events.Event

  import flash.display.MovieClip;

  public class Platform extends MovieClip {

  var platformSpeed;

  var platformWidth;

  public function Platform() {

  this.addEventListener(Event.ENTER_FRAME, loop)

  this.x = 1280;

  this.y = Math.floor(Math.random() * 550 + 200);

  this.platformSpeed = Math.random() * 8 + 6;

  this.platformCollision.width = (Math.random() * 400 + 200)

  platformWidth = this.platformCollision.width;

  if (Math.random() > 0.5) {

  var onion = new Onion(this.platformSpeed, this.platformWidth);

  this.platformContainer.addChild(onion);

  }

  }

  public function loop(e: Event): void {

  this.x -= platformSpeed;

  }

  public function removeSelf(): void {

  this.removeEventListener(Event.ENTER_FRAME, loop);

  this.parent.removeChild(this);

  }

  }

}

I tried

import com.Platform

in the onion class and using the platform.width but I got the error "1172: Definition com:Platform could not be found."

This topic has been closed for replies.
Correct answer kglad

use:

package {

  import flash.display.MovieClip;

  import flash.events.Event;

  public class Onion extends MovieClip {

  var onionSpeed;

  public function Onion(_onionSpeed, pWidth) {

onionSpeed=_onionSpeed;

  this.addEventListener(Event.ENTER_FRAME, loop);

  this.x += pWidth;

  }

  function loop(e: Event): void {

  this.x -= onionSpeed;

}

}

}

p.s. you should type your variables.

2 replies

Participant
August 9, 2015

I'm unsure how this fixed the problem, but thank you very much.

kglad
Community Expert
Community Expert
August 9, 2015

your onionSpeed was undefined outside of the constructor.  i fixed that.

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
August 9, 2015

use:

package {

  import flash.display.MovieClip;

  import flash.events.Event;

  public class Onion extends MovieClip {

  var onionSpeed;

  public function Onion(_onionSpeed, pWidth) {

onionSpeed=_onionSpeed;

  this.addEventListener(Event.ENTER_FRAME, loop);

  this.x += pWidth;

  }

  function loop(e: Event): void {

  this.x -= onionSpeed;

}

}

}

p.s. you should type your variables.