Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Flash swf missing external image

    I have created a sliding puzzle in Flash CS3, Actionscript 3.0. It works perfectly until I upload and publish it. The SWF file loads ok but the image for the puzzle is missing. I'm new to this so am not sure if the problem is in the javascript...

    Code:
    <script type="text/javascript" src="/img/flash/swfobject.js"</script>
    <div class="ContactPage"
    <div id="SlidingPuzzle"></div>
    <script type="text/javascript">
       var so = new SWFObject("/img/gemsfriends/Puzzles/SlidingPuzzle.swf", "SlidingPuzzleID", "100%", "100%;", "9", "");
       so.addParam("wmode");
       so.addParam("scale","noscale");
       so.write("SlidingPuzzle");
    </script>
    </div>
    or the Actionscript...

    Code:
    public function startSlidingPuzzle() {
    			// blank spot is the bottom right
    			blankPoint = new Point(numPiecesHoriz-1,numPiecesVert-1);
    
    			// load the bitmap
    			loadBitmap("slidingimage.jpg");
    		}
    		var myRequest:URLRequest = new URLRequest("/img/gemsfriends/Puzzles/slidingimage.jpg");
    		var myLoader:Loader = new Loader();
    
    
    		// get the bitmap from an external source
    		public function loadBitmap(bitmapFile:String) {
    			var loader:Loader = new Loader();
    			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingDone);
    			var request:URLRequest = new URLRequest(bitmapFile);
    			loader.load(request);
    		}
    
    		// bitmap done loading, cut into pieces
    		public function loadingDone(event:Event):void {
    Any help or advice would be appreciated.
    Many thanks in advance.

  • #2
    Senior Coder gnomeontherun's Avatar
    Join Date
    Sep 2007
    Location
    Houston
    Posts
    2,846
    Thanks
    10
    Thanked 238 Times in 229 Posts
    Check the path to the image on this line

    Code:
    var myRequest:URLRequest = new URLRequest("/img/gemsfriends/Puzzles/slidingimage.jpg");
    Perhaps try putting the exact URL?
    jeremy - gnomeontherun
    Educated questions often get educated answers, and simple questions often get simple answers.

  • #3
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your response - I tried the full URL but without any luck.

    This is the original Actionscript used, I'm not sure how I ended up with var myRequest in there but it wasn't supposed to be in the file. Still not loading the jpg which makes me wonder if it's a javascript issue as it doesn't know to look for it.


    Code:
    package {
    	import flash.display.*;
    	import flash.events.*;
    	import flash.net.URLRequest;
    	import flash.geom.*;
    	import flash.utils.Timer;
    
    	public class SlidingPuzzle extends MovieClip {
    		// space between pieces and offset
    		static const pieceSpace:Number = 0;
    		static const horizOffset:Number = 255;
    		static const vertOffset:Number = 75;
    
    		// number of pieces
    		static const numPiecesHoriz:int = 3;
    		static const numPiecesVert:int = 3;
    
    		// random shuffle steps
    		static const numShuffle:int = 200;
    
    		// animation steps and time
    		static const slideSteps:int = 10;
    		static const slideTime:int = 250;
    
    		// size of pieces
    		private var pieceWidth:Number;
    		private var pieceHeight:Number;
    
    		// game pieces
    		private var puzzleObjects:Array;
    
    		// tracking moves
    		private var blankPoint:Point;
    		private var slidingPiece:Object;
    		private var slideDirection:Point;
    		private var slideAnimation:Timer;
    
    		public function startSlidingPuzzle() {
    			// blank spot is the bottom right
    			blankPoint = new Point(numPiecesHoriz-1,numPiecesVert-1);
    
    			// load the bitmap
    			loadBitmap("slidingimage.jpg");
    		}
    		
    		// get the bitmap from an external source
    		public function loadBitmap(bitmapFile:String) {
    			var loader:Loader = new Loader();
    			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingDone);
    			var request:URLRequest = new URLRequest(bitmapFile);
    			loader.load(request);
    		}
    
    		// bitmap done loading, cut into pieces
    		public function loadingDone(event:Event):void {
    
    			// create new image to hold loaded bitmap
    			var image:Bitmap = Bitmap(event.target.loader.content);
    			pieceWidth = image.width/numPiecesHoriz;
    			pieceHeight = image.height/numPiecesVert;
    
    			// cut into puzzle pieces
    			makePuzzlePieces(image.bitmapData);
    
    			// shuffle them
    			shufflePuzzlePieces();
    		}
    		// cut bitmap into pieces
    		public function makePuzzlePieces(bitmapData:BitmapData) {
    			puzzleObjects = new Array();
    			for (var x:uint=0; x<numPiecesHoriz; x++) {
    				for (var y:uint=0; y<numPiecesVert; y++) {
    					// skip blank spot
    					if (blankPoint.equals(new Point(x,y))) {
    						continue;
    					}
    
    					// create new puzzle piece bitmap and sprite
    					var newPuzzlePieceBitmap:Bitmap = new Bitmap(new BitmapData(pieceWidth,pieceHeight));
    					newPuzzlePieceBitmap.bitmapData.copyPixels(bitmapData,new Rectangle(x*pieceWidth,y*pieceHeight,pieceWidth,pieceHeight),new Point(0,0));
    					var newPuzzlePiece:Sprite = new Sprite();
    					newPuzzlePiece.addChild(newPuzzlePieceBitmap);
    					addChild(newPuzzlePiece);
    
    					// set location
    					newPuzzlePiece.x = x*(pieceWidth+pieceSpace) + horizOffset;
    					newPuzzlePiece.y = y*(pieceHeight+pieceSpace) + vertOffset;
    
    					// create object to store in array
    					var newPuzzleObject:Object = new Object();
    					newPuzzleObject.currentLoc = new Point(x,y);
    					newPuzzleObject.homeLoc = new Point(x,y);
    					newPuzzleObject.piece = newPuzzlePiece;
    					newPuzzlePiece.addEventListener(MouseEvent.CLICK,clickPuzzlePiece);
    					puzzleObjects.push(newPuzzleObject);
    				}
    			}
    		}
    		// make a number of random moves
    		public function shufflePuzzlePieces() {
    			for (var i:int=0; i<numShuffle; i++) {
    				shuffleRandom();
    			}
    		}
    		// random move
    		public function shuffleRandom() {
    			// loop to find valid moves
    			var validPuzzleObjects:Array = new Array();
    			for (var i:uint=0; i<puzzleObjects.length; i++) {
    				if (validMove(puzzleObjects[i]) != "none") {
    					validPuzzleObjects.push(puzzleObjects[i]);
    				}
    			}
    			// pick a random move
    			var pick:uint = Math.floor(Math.random()*validPuzzleObjects.length);
    			movePiece(validPuzzleObjects[pick],false);
    		}
    
    		public function validMove(puzzleObject:Object):String {
    			// is the blank spot above
    			if ((puzzleObject.currentLoc.x == blankPoint.x) &&
    			(puzzleObject.currentLoc.y == blankPoint.y+1)) {
    				return "up";
    			}
    			// is the blank spot below
    			if ((puzzleObject.currentLoc.x == blankPoint.x) &&
    			(puzzleObject.currentLoc.y == blankPoint.y-1)) {
    				return "down";
    			}
    			// is the blank to the left
    			if ((puzzleObject.currentLoc.y == blankPoint.y) &&
    			(puzzleObject.currentLoc.x == blankPoint.x+1)) {
    				return "left";
    			}
    			// is the blank to the right
    			if ((puzzleObject.currentLoc.y == blankPoint.y) &&
    			(puzzleObject.currentLoc.x == blankPoint.x-1)) {
    				return "right";
    			}
    			// no valid moves
    			return "none";
    		}
    		// puzzle piece clicked
    		public function clickPuzzlePiece(event:MouseEvent) {
    			// find piece clicked and move it
    			for (var i:int=0; i<puzzleObjects.length; i++) {
    				if (puzzleObjects[i].piece == event.currentTarget) {
    					movePiece(puzzleObjects[i],true);
    					break;
    				}
    			}
    		}
    		// move a piece into the blank space
    		public function movePiece(puzzleObject:Object, slideEffect:Boolean) {
    			// get direction of blank space
    			switch (validMove(puzzleObject)) {
    				case "up" :
    					movePieceInDirection(puzzleObject,0,-1,slideEffect);
    					break;
    				case "down" :
    					movePieceInDirection(puzzleObject,0,1,slideEffect);
    					break;
    				case "left" :
    					movePieceInDirection(puzzleObject,-1,0,slideEffect);
    					break;
    				case "right" :
    					movePieceInDirection(puzzleObject,1,0,slideEffect);
    					break;
    			}
    		}
    		// move the piece into the blank spot
    		public function movePieceInDirection(puzzleObject:Object, dx,dy:int, slideEffect:Boolean) {
    			puzzleObject.currentLoc.x += dx;
    			puzzleObject.currentLoc.y += dy;
    			blankPoint.x -= dx;
    			blankPoint.y -= dy;
    
    			// animate or not
    			if (slideEffect) {
    				// start animation
    				startSlide(puzzleObject,dx*(pieceWidth+pieceSpace),dy*(pieceHeight+pieceSpace));
    			} else {
    				// no animation, just move
    				puzzleObject.piece.x = puzzleObject.currentLoc.x*(pieceWidth+pieceSpace) + horizOffset;
    				puzzleObject.piece.y = puzzleObject.currentLoc.y*(pieceHeight+pieceSpace) + vertOffset;
    			}
    		}
    		// set up a slide
    		public function startSlide(puzzleObject:Object, dx, dy:Number) {
    			if (slideAnimation != null) {
    				slideDone(null);
    			}
    			slidingPiece = puzzleObject;
    			slideDirection = new Point(dx,dy);
    			slideAnimation = new Timer(slideTime/slideSteps,slideSteps);
    			slideAnimation.addEventListener(TimerEvent.TIMER,slidePiece);
    			slideAnimation.addEventListener(TimerEvent.TIMER_COMPLETE,slideDone);
    			slideAnimation.start();
    		}
    		// move one step in slide
    		public function slidePiece(event:Event) {
    			slidingPiece.piece.x += slideDirection.x/slideSteps;
    			slidingPiece.piece.y += slideDirection.y/slideSteps;
    		}
    		// complete slide
    		public function slideDone(event:Event) {
    			slidingPiece.piece.x = slidingPiece.currentLoc.x*(pieceWidth+pieceSpace) + horizOffset;
    			slidingPiece.piece.y = slidingPiece.currentLoc.y*(pieceHeight+pieceSpace) + vertOffset;
    			slideAnimation.stop();
    			slideAnimation = null;
    
    			// check to see if puzzle is complete now
    			if (puzzleComplete()) {
    				clearPuzzle();
    				gotoAndStop("gameover");
    			}
    		}
    		// check to see if all pieces are in place
    		public function puzzleComplete():Boolean {
    			for (var i:int=0; i<puzzleObjects.length; i++) {
    				if (!puzzleObjects[i].currentLoc.equals(puzzleObjects[i].homeLoc)) {
    					return false;
    				}
    			}
    			return true;
    		}
    		// remove all puzzle pieces
    		public function clearPuzzle() {
    			for (var i in puzzleObjects) {
    				puzzleObjects[i].piece.addEventListener(MouseEvent.CLICK,clickPuzzlePiece);
    				removeChild(puzzleObjects[i].piece);
    			}
    			puzzleObjects = null;
    		}
    	}
    }

  • #4
    Senior Coder gnomeontherun's Avatar
    Join Date
    Sep 2007
    Location
    Houston
    Posts
    2,846
    Thanks
    10
    Thanked 238 Times in 229 Posts
    Its not a javascript issue, if the SWF loads that is all that the JS controls in this case. You aren't passing any flashvars to Flash, so thats not the issue.

    Code:
    loadBitmap("slidingimage.jpg");
    You have this hard coded into the actionscript, is this the path to the file? (from the SWF file!!!)? So is it in the exact same directory as the SWF?
    jeremy - gnomeontherun
    Educated questions often get educated answers, and simple questions often get simple answers.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •