Skip to main content
Known Participant
November 3, 2010
Answered

AS3 two arrays targeting same destination - how to?

  • November 3, 2010
  • 1 reply
  • 1017 views

I need help with arrays. I have a project like a book. Each table of content title is linked to a page. At the same time the user will have all page numbers available to navigate at will. I have been able to work links or pages, but not both together.

HERE IS MY CODE

stop();

PAGES ARRAY

var pagesArray:Array = [page1_1, page1_2, page1_3, page1_4, page1_5, page1_6, page1_7, page1_8, page1_9, page1_10,
      page1_11, page1_12, page1_13, page1_14, page1_15, page1_16, page1_17, page1_18, page1_19,page1_20,
      page1_21, page1_22, page1_23, page1_24, page1_25];

TABLE OF CONTENT ARRAY

var linksArray:Array = [link_1, link_2, link_3, link_4, link_5, link_6, link_7, link_8, link_9, link_10,
      link_11, link_12,link_13, link_14,link_15, link_16, link_17, link_18, link_19,link_20,
      link_21, link_22, link_23];

FRAME LABELS ARRAY

var paginationArray:Array = ["chap1_1", "chap1_2", "chap1_3", "chap1_4", "chap1_5", "chap1_6", "chap1_7", "chap1_8", "chap1_9", "chap1_10",
        "chap1_11", "chap1_12", "chap1_13", "chap1_14", "chap1_15", "chap1_16", "chap1_17", "chap1_18", "chap1_19", "chap1_20",
        "chap1_21", "chap1_21", "chap1_22", "chap1_23"];


var bothArrays:Array = [pagesArray, linksArray];

for (var q:int = 0; q < bothArrays.length; q++) {
bothArrays.buttonMode = true;
bothArrays.addEventListener(MouseEvent.CLICK, activeHandler60);
}

function activeHandler60(event:MouseEvent):void {
for (var q:int = 0; q < bothArrays.length; q++) {
  if (event.currentTarget == bothArrays) {
   this.gotoAndStop(paginationArray);
   bothArrays.mouseEnabled = false;
   bothArrays.alpha = 0.2;
   page1_1.mouseEnabled=true;
   page1_1.alpha=1;
    
   } else {
   bothArrays.mouseEnabled = true;
   linksArray.alpha = 1;
  }
}
}

// LABELING BUTTONS

page1_1.txt1.text="1"; page1_2.txt1.text="2"; page1_3.txt1.text="3"; page1_4.txt1.text="4"; page1_5.txt1.text="5";
page1_6.txt1.text="6"; page1_7.txt1.text="7"; page1_8.txt1.text="8"; page1_9.txt1.text="9"; page1_10.txt1.text="10";
page1_11.txt1.text="11"; page1_12.txt1.text="12"; page1_13.txt1.text="13"; page1_14.txt1.text="14"; page1_15.txt1.text="15";
page1_16.txt1.text="16"; page1_17.txt1.text="17"; page1_18.txt1.text="18"; page1_19.txt1.text="19"; page1_20.txt1.text="20";
page1_21.txt1.text="21"; page1_22.txt1.text="22"; page1_23.txt1.text="23"; page1_24.txt1.text="24"; page1_25.txt1.text="25";


Thanks for your help.

German

This topic has been closed for replies.
Correct answer kglad

Thanks for replying Kglad.

Let's see if this helps clarify the project. It is like a book. The first page shows the table of content. Each item of the table is linked to its destination page (frame labels). Once the user jumps from table of content to any page, on top of all pages there are buttons (each linked to each frame label), so that s/he can go to any page without sequential order. Both table of content and page buttons go to the same frame labels. Does this make sense?

Thanks again.

German


so, if i were going to encode that i would probably create my chapter movieclip buttons, eg:

linkchap_1,...,linkchap_N where i have chapters 1,...,N

each linkchap button would add a different movieclip to the displaylist.  eg, clicking linkchap_x button would cause chap_x movieclip to be added to the display list.  and chap_x's first frame would contain a stop() and movieclip buttons linkpage_1,...,linkpage_M where chap_x has pages 1,...,M and the content of chap_x page y is on frame 5+y.  ie, frame 1 has the linkpage buttons, nothing is on frames 2,3,4,5 and frame 6 contains page 1, frame 2 contains page 2,...,frame 5+M contains the contents of page M.

now, you're setup to code all your linkchap and linkpage buttons and you don't need any arrays or frame labels.:

var tl:MovieClip=this;

var book:MovieClip = new MovieClip();

addChild(book);

// position book.  (this is where the chapters/pages will be displayed)

for(var i:uint=1;i<=N;i++){

tl["linkchap_"+i].ivar=i;

tl["linkchap_"+i].addEventListener(MouseEvent.CLICK,chapterF);

tl["linkchap_"+i].buttonMode=true;

}

function chapterF(e:Event):void{

removePreviousChapterF();

var classRef:Class = Class(getDefinitionByName("chap_"+(e.currentTarget).ivar));

var chapter:MovieClip = new classRef();

book.addChild(chapter);

addPageListenersF(chapter);

}

function removePreviousChapterF():void{

var chapter:Movieclip = MovieClip(book.getChildAt(0));

removePageListenersF(chapter);

book.removeChild(chapter);

chapter=null

}

function addPageListenersF(chapter:MovieClip):void{

for(var i:uint=0;i<chapter.numChildren;i++){

var dobj:DisplayObject=chapter.getChildAt(i);

if(dobj is MovieClip){

dob.ivar=i;

dobj.addEventListener(MouseEvent.CLICK,pageF);

}

}

}

function removePageListenersF(chapter:MovieClip):void{

chapter.gotoAndStop(1);

for(var i:uint=0;i<chapter.numChildren;i++){

var dobj:DisplayObject=chapter.getChildAt(i);

if(dobj is MovieClip){

dobj.removeEventListener(MouseEvent.CLICK,pageF);

}

}

}

function pageF(e:Event):void{

var linkpage:MovieClip=MovieClip(e.currentTarget);

var chapter:MovieClip=MovieClip(linkpage.parent);

chapter.gotoAndStop(linkpage.ivar);

}

1 reply

kglad
Community Expert
Community Expert
November 3, 2010

bothArrays has length two.  each element of bothArrays is an array and you can't apply interactive object methods/properties to an array.

perhaps you want to do something like:

german01 wrote:


PAGES ARRAY

var pagesArray:Array = [page1_1, page1_2, page1_3, page1_4, page1_5, page1_6, page1_7, page1_8, page1_9, page1_10,
      page1_11, page1_12, page1_13, page1_14, page1_15, page1_16, page1_17, page1_18, page1_19,page1_20,
      page1_21, page1_22, page1_23, page1_24, page1_25];

TABLE OF CONTENT ARRAY

var linksArray:Array = [link_1, link_2, link_3, link_4, link_5, link_6, link_7, link_8, link_9, link_10,
      link_11, link_12,link_13, link_14,link_15, link_16, link_17, link_18, link_19,link_20,
      link_21, link_22, link_23];

FRAME LABELS ARRAY

var paginationArray:Array = ["chap1_1", "chap1_2", "chap1_3", "chap1_4", "chap1_5", "chap1_6", "chap1_7", "chap1_8", "chap1_9", "chap1_10",
        "chap1_11", "chap1_12", "chap1_13", "chap1_14", "chap1_15", "chap1_16", "chap1_17", "chap1_18", "chap1_19", "chap1_20",
        "chap1_21", "chap1_21", "chap1_22", "chap1_23"];


var bothArrays:Array = [pagesArray, linksArray];

for (var q:int = 0; q < bothArrays.length; q++) {

for(var i:uint=0;i<bothArrays.length;i++){
bothArrays.buttonMode = true;
bothArrays.addEventListener(MouseEvent.CLICK, activeHandler60);

}
}

function activeHandler60(event:MouseEvent):void {
for (var q:int = 0; q < bothArrays.length; q++) {

for(var i:uint=0;i<bothArrays.length;i++){
  if (event.currentTarget == bothArrays) {
  // you should be using q and i to do something

}

}
}


german01Author
Known Participant
November 3, 2010

Kglad:

I incorporated your suggestions as shown in the function below. However, while links may seem to work, page numbers go only to page 1.  I added the line linksArray.visible=false;  because links are only in one frame while buttons are across the timelime. It still doesn't work the way it should. Can you please advise?


function activeHandler60(event:MouseEvent):void {

for (var q:int = 0; q < bothArrays.length; q++) {
for(var i:uint=0; i<bothArrays.length; i++){

if (event.currentTarget == bothArrays) {
  this.gotoAndStop(paginationArray);
  bothArrays.mouseEnabled=false;
  bothArrays.alpha=0.2;
  linksArray.visible=false;
 
  }
  else {
bothArrays.mouseEnabled=true;
bothArrays.alpha=1;
  }

}
}

}

kglad
Community Expert
Community Expert
November 3, 2010

it's not clear what logic should be used to associate a bothArrays element with a paginationArray element but this is the most likely association (but it still needs some work):



function activeHandler60(event:MouseEvent):void {

for (var q:int = 0; q < bothArrays.length; q++) {
for(var i:uint=0; i<bothArrays.length; i++){

if (event.currentTarget == bothArrays) {
  this.gotoAndStop(paginationArray);
  bothArrays.mouseEnabled=false;
  bothArrays.alpha=0.2;
  linksArray.visible=false;
 
  }
  else {
bothArrays.mouseEnabled=true;
bothArrays.alpha=1;
  }

}
}

}