Skip to main content
saratogacoach
Inspiring
October 30, 2010
Answered

adding URL-check to AS file

  • October 30, 2010
  • 1 reply
  • 1664 views

Hi,

Moved from general Flash forum

I am a newbie using Flash Pro CS5 and have beginner skills in AS3.

I am working on an eLearning lesson and already, with a lot of earlier help of others, and have an Action Script file to use for a white noise  animation in a learning lesson.

I need to make a couple of additions but am very unfamiliar with adding  script to an Action Script file "package." Specifically, I want to add a  URL checking to the Action Script file so that students do not view the  Flash movie animation outside of the lesson which contains learning  materials and instructions for using the animation.

I would like to add the following script but so far, I have been getting error messages:

if ((this.loaderInfo.url).indexOf("mywebsite. com") == -1)
            navigateToURL (new URLRequest ("http://mywebsite.com/default.html"));
            else
//run the animation

The current AS3 in the .as file is:

package
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.TimerEvent;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.utils.getTimer;
    import flash.utils.Timer;
   
    public class Main extends Sprite
    {
        private var viewbmd:BitmapData;
        private var workbmd:BitmapData;
        private var scaleUp:Matrix;
        private var px:Vector.<uint>;
        private var timer:Timer;
        private var itsNoisy:Boolean;
       
        public function Main():void
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
       
        private function init(e:Event = null):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            viewbmd = new BitmapData(stage.stageWidth, stage.stageHeight, false);
            var view:Bitmap = new Bitmap(viewbmd);
            addChild(view);
            workbmd = new BitmapData(viewbmd.width / 8, viewbmd.height / 8, false);
            scaleUp = new Matrix();
            scaleUp.scale(8, 8);
           
            itsNoisy = true;
           
            stage.addEventListener(MouseEvent.CLICK, manageNoise);
           
            initNoise();
            timer = new Timer(50);
            timer.addEventListener(TimerEvent.TIMER, onTick);
            timer.start();
        }
       
        private function initNoise():void {
            var array:Array = [];
            for (var i:uint = 0; i < 255; i++) {
                array = 0xffffff * (i % 2);
            }
            workbmd.noise(getTimer(), 0, 255, 7, true);
            workbmd.paletteMap(workbmd, workbmd.rect, new Point(0, 0), array, array, array);
            px = workbmd.getVector(workbmd.rect);
        }
       
        private function onTick(e:Event):void {
            flipSomePixels(160);
            viewbmd.draw(workbmd, scaleUp);
        }

        private function flipSomePixels(howMany:int):void {
            for (var i:int = 0; i < howMany; i++){
            var pxidx:int = Math.floor(Math.random()*px.length);
                px[pxidx] = ~px[pxidx]; //bitwise NOT to flip all bits
            }
            workbmd.setVector(workbmd.rect, px);
        }
       
        private function manageNoise(e:MouseEvent):void {
            itsNoisy ? timer.removeEventListener(TimerEvent.TIMER, onTick) : timer.addEventListener(TimerEvent.TIMER, onTick);
            itsNoisy = !itsNoisy;
        }
       
       
       
    }
   
}

If anyone can take a look and please let me know how to add the  additional script to check for the URL (please be specific since I am a beginner in  scripting):

I have tried everything that I can think of and am stuck.

Thanks in advance for your help.

Kind Regards,

saratogacoach

This topic has been closed for replies.
Correct answer Andrei1-bKoviI

Hi Andrei1,

Still stuck.

I found online that the following may possibly be the script item, but I am not sure if it works with AS3. So far, when adapted into my AS file, I get an error.

var domain:String = "http://www.yourdomain.com" //enter your domain name here 
if(_root._url.indexOf(domain) == -1 )
{
   getURL(domain, "_self");
}

The error is

C:\Users\Stephen\Desktop\FlashWhiteNoise\URLexp\Main.as, Line 33    1119: Access of possibly undefined property _url through a reference with static type flash.display:DisplayObject.

Not sure what to import/instantiate or how to fix this.

Kind Regards,


Again, loaderInfo.url is where swf is hosted and there is no change to it. So, in the context of what you are trying to accomplish, it makes no sense to check it for it will be always the same unless you start hosting swf elsewhere.

Chek out my suggestion with ExternalInterface.

1 reply

Inspiring
October 30, 2010

If you want to bump them off in the very beginning the following should work:

private function init(e:Event = null):void
{
     if ((this.loaderInfo.url).indexOf("mywebsite.com") == -1) {
          navigateToURL (new URLRequest ("http://mywebsite.com/default.html"), "_self");
     }
     else {
          removeEventListener(Event.ADDED_TO_STAGE, init);
          viewbmd = new BitmapData(stage.stageWidth, stage.stageHeight, false);
          var view:Bitmap = new Bitmap(viewbmd);
          addChild(view);
          workbmd = new BitmapData(viewbmd.width / 8, viewbmd.height / 8, false);
          scaleUp = new Matrix();
          scaleUp.scale(8, 8);
          itsNoisy = true;
          stage.addEventListener(MouseEvent.CLICK, manageNoise);
          initNoise();
          timer = new Timer(50);
          timer.addEventListener(TimerEvent.TIMER, onTick);
          timer.start();
     }
}

saratogacoach
Inspiring
October 30, 2010

Hi Andrei1,

Thanks for your reply and suggestion.

I pasted this into the AS file (.as) and ran it but got error messages:

C:\Users\Stephen\Desktop\FlashWhiteNoise\URLexp\Main.as, Line 32    1180: Call to a possibly undefined method navigateToURL.
C:\Users\Stephen\Desktop\FlashWhiteNoise\URLexp\Main.as, Line 32    1180: Call to a possibly undefined method URLRequest.

I must be doing something wrong.

Kind Regards,

saratogacoach

Inspiring
October 30, 2010

You have to import navigateToURL in the class header:

import flash.net.navigateToURL