Slider won't work in class

New Here ,
Feb 11, 2021

Copy link to clipboard

Copied

This is the Error;

TypeError: Error #1010: A term is undefined and has no properties. at MethodInfo-202() at flash.utils::Timer/_timerDispatch() at flash.utils::Timer/tick()

 


This is my code; 

package {
import fl.controls.Slider;
import fl.events.SliderEvent;
import fl.controls.NumericStepper;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.display.Stage;
import flash.utils.Timer;
import flash.events.TimerEvent;

public class Fish extends Sprite {
public var gulFisk: Array = [];
public var sortFisk: Array = [];
public var sl: Slider = new Slider();
public var antall: int = 0;

public function Fish(stg: Object) {
// constructor code

//Legger til en slider
stg.addChild(sl);
sl.x = 500;
sl.y = 50;
sl.width = 200;
sl.snapInterval = 1;
sl.tickInterval = 1;
sl.maximum = 10;
sl.minimum = 1;
sl.value = 1;

antall = sl.value;

for (var i: int = 0; i < antall; i++) {
var yelFisk: Sprite = new GULFISK;
var blaFisk: Sprite = new SORTFISK;
gulFisk.push(yelFisk);
sortFisk.push(blaFisk);
stg.addChild(gulFisk[i]);
stg.addChild(sortFisk[i]);

yelFisk.x = 175;
yelFisk.y = 260 + 50 * i;
yelFisk.width = 70;
yelFisk.height = 60;
blaFisk.x = 590;
blaFisk.y = 260 + 50 * i;
blaFisk.width = 70;
blaFisk.height = 60;
}

trace("Hei fra klassen som heter Fisk!");
}

// ------ Mine egne metoder, og funksjoner -------
////////////////////////////////////////////////////////////////////////////

public function timer() {
var tTimer: Timer = new Timer(1000);
tTimer.addEventListener(TimerEvent.TIMER, onTimer);
tTimer.start(); // koble til knapp - START

function onTimer (event:TimerEvent): void{
sortFisk[1].x+=10;
gulFisk[1].x+=10;
}
}

////////////////////////////////////////////////////////////////////////////

public function randomBetween(a: uint, b: uint): Number {
return (a + (b - a) * Math.random());
}

////////////////////////////////////////////////////////////////////////////

 

////////////////////////////////////////////////////////////////////////////

 

////////////////////////////////////////////////////////////////////////////

}

}

Views

45

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

1 Correct Answer

Adobe Community Professional , Feb 11, 2021
JoãoCésar Adobe Community Professional , Feb 11, 2021
Hi.   Change: sortFisk[1].x+=10; gulFisk[1].x+=10;   To: sortFisk[0].x+=10; gulFisk[0].x+=10;   The two arrays have only one element in each and the index of the first element in an array is 0.   Regards, JC  

Likes

Translate

Translate
Jump to answer Jump to answer
Adobe Community Professional ,
Feb 11, 2021

Copy link to clipboard

Copied

Hi.

 

Change:

sortFisk[1].x+=10;
gulFisk[1].x+=10;

 

To:

sortFisk[0].x+=10;
gulFisk[0].x+=10;

 

The two arrays have only one element in each and the index of the first element in an array is 0.

 

Regards,

JC

 

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
kglad LATEST
Adobe Community Professional ,
Feb 11, 2021

Copy link to clipboard

Copied

hard coding those array elements is a bad idea, in general. plus, you'll eventually want to account for larger (than 1) array sizes (or there's no proint in using arrays).

 

so use:

 

function onTimer (event:TimerEvent): void{

for (var i: int = 0; i < sortFisk.length; i++)
sortFisk[i].x+=10;

// there should be an if statement about what to do when .x > stage.stageWidth
}

for (i = 0; i < gulFisk.length; i++)

gulFisk[i].x+=10;

// there should be an if statement about what to do when .x > stage.stageWidth

}
}

 

p.s.  these:

var yelFisk: Sprite = new GULFISK;
var blaFisk: Sprite = new SORTFISK;

 

should be:

 

var yelFisk: Sprite = new GULFISK();
var blaFisk: Sprite = new SORTFISK();

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more