Skip to main content
Participating Frequently
June 18, 2014
Answered

Problem with persistence across scenes

  • June 18, 2014
  • 1 reply
  • 231 views

I have a student who has created some code- see below. At the moment this code is in Scene 5 - but the blocks also appear on other scenes.

  1. How does he limit this code to just one scene?
  2. Can he replicate this to be used on other scenes, but starting out fresh.

Currently, 50 blocks spawn on top of one another. These are used for teaching addition or multiplication. When they've been dragged out into a pattern, we don't want that pattern on another scene, we want them on on top of the other.

The code is not overly elegant, but it does work - except for being repeated across scenes.

block = []

//this function creates a block at a certain location and adds it to an array

//new identifiers are block30 and so on

function spawnBlock(){

  blockSpawn = attachMovie("block","block"+_root.getNextHighestDepth(),_root.getNextHighestDepth(),{_x:550,_y:300});

  block.push(blockSpawn._name);

}

//this runs the spawnBlock funtion 50 times creating 50 new blocks

//change 50 to the number of blocks desired

for(i=0;i<50;i++){

  spawnBlock();

  //trace(block); //testing if the funtion was working

}

function dragSetup(clip){ //this is used to assign a movie clip to funtion that can be reused with each block that is created

  clip.onPress = function(){

  startDrag(this);

  }

  clip.onRelease = clip.onReleaseOutside=function(){ //same as above

  stopDrag();

  }

}

//this lets each block that is needed to be dragged

//add in more lines to let more blocks get dragged

dragSetup(block0);

dragSetup(block1);

dragSetup(block2);

dragSetup(block3);

dragSetup(block4);

dragSetup(block5);

dragSetup(block6);

dragSetup(block7);

dragSetup(block8);

dragSetup(block9);

remainder removed for brevity.

This topic has been closed for replies.
Correct answer kglad

use:

function clear_blockF():Void{

for(var i:Number=block.length-1;i>=0;i--){

this[block].removeMovieClip();

}

block.length=0;

}

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
June 18, 2014

use:

function clear_blockF():Void{

for(var i:Number=block.length-1;i>=0;i--){

this[block].removeMovieClip();

}

block.length=0;

}

basil01Author
Participating Frequently
June 20, 2014

Thanks again kglad. You're a saviour.

kglad
Community Expert
Community Expert
June 20, 2014

you're welcome.