Skip to main content
Participant
February 11, 2021
Answered

Slider won't work in class

  • February 11, 2021
  • 2 replies
  • 296 views

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());
}

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

 

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

 

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

}

}

    This topic has been closed for replies.
    Correct answer JoãoCésar17023019

    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

     

    2 replies

    kglad
    Community Expert
    Community Expert
    February 11, 2021

    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();

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    February 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