Unofficial Flash Challenges 2

by BetaWar

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1890 Views - Last Post: 25 August 2008 - 03:48 PM

#1 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1134
  • View blog
  • Posts: 7,094
  • Joined: 07-September 06

Unofficial Flash Challenges 2

Post icon  Posted 09 August 2008 - 09:21 PM

Well due to the long amount of time since the last Flash CHallenges were posted I thought that I would start a new unofficial topic with a new set of them to test your skills on. Unlike the previous challenge we will be using this topic to post your solutions so if you don't want to see people's answers to the challenges don't scroll down the page too far. NOTE - If you have trouble with sections of any of these challenges please start a NEW topic with Flash Challenges 2 as the Topic Description, that way I, or any of the other Flash gurus can come and help you asap. Don't post requests for help in this topic, they WON'T be answered.

This first challenge will be based off of a lot of questions that I have seen in the Flash section recently: Movieclips following Mice and other Movieclips, as well as turning. The challenges will start off easy and get progressively more difficult to accomplish on the way down.

Our Challenge Rules
1. Use ActionScript and as few library objects as possible.
* If you are advanced at Actioscript try and complete the challenge totally out of actionscript without any required library objects. You can tell if you have successfully completed this by opening a new Flash file and just pasting the Actionscript into it: If it works it is correct.
2. Go at your own pace, don't panic and remember that there are people here who are willing to help if you get stuck.
3. When using a library object make sure you tell us that in the documentation or post.
4. If somebody has already posted a solution to one of the challenges:
* Beginners: Try to do your own script from scratch using an exsisting solution as a guide.
* !Beginners: Either do your own "better" version or simplify an existing solution.
5. Post your solutions to ALL the parts you've attempted, not just the last one.

The Challenge
1 - Create a circle with radius 25. Create another circle with radius 5 and place it (centered) on the right side of the first circle. Have it so that when you press the left or right keys the small circle rotates around the large circle by 5 degrees.
HINT - The easiest way to do this is to place both circles inside of teh same parent movieclip and rotate the parent MC
2 - Create the circles as in part 1, and then change them to rotate to face the mouse depending on the mouse's position on the screen relative to the circles.
3 - Create the 2 cirles as in part 1, but this time also create a square movieclip that moves up, down, left and right when you press the keys (respecively). Have the circles rotate to face the square (little circle should be aligned with the square).
4 - Create 2 instance s of the circles as in part 1 and a square. Place instance of the circles in the top left corner and the other in the top right. Have the circles rotate to face the square and have the square take arrow key input to move, however add a gravity affect to make the square slowly drift to the bottom of the screen (however stay on screen at all times) if the up arrow is not being pressed.
5 - (Holy Shit Level) Create 2 instances of the circles, one placed in the top left and the other placed in the top right. Create a quare that is placed in the bottom left and is effected by the circles as if they are magnets (it will drift towards them) however make sure that the script takes into account the amount of distance between the square and itself: the smaller the distacne the stronger the magnetic pull will be. Have the 2 circles rotate to face the square at all times. The square should no be able to overlap the circles, but if it collides with them it should act like it has hit a solit surface and stop. The square should also eventually come to rest on one of the instances of the circles (at which point it stops moving).

Have fun with the challenges and don't wrack your brains too badly. Post your results here too.

Is This A Good Question/Topic? 0
  • +

Replies To: Unofficial Flash Challenges 2

#2 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Unofficial Flash Challenges 2

Posted 10 August 2008 - 07:09 PM

Question in regards to challenge 1, is the smaller circle inside the larger circle (i.e. inscribed) or is it on the outside, tangent to the larger circle?
Was This Post Helpful? 0
  • +
  • -

#3 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1134
  • View blog
  • Posts: 7,094
  • Joined: 07-September 06

Re: Unofficial Flash Challenges 2

Posted 10 August 2008 - 07:16 PM

It doesn't really matter, the smaller circle is just being used to be able to tell which way the circle is facing. So lets say that it is tangent, but if you want it to be inscribed that is fine as long as you can tell that is isn't centered.
Was This Post Helpful? 0
  • +
  • -

#4 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Unofficial Flash Challenges 2

Posted 10 August 2008 - 07:50 PM

Challenge 1
Paste in Frame 1 of the 400px x 300px flash document
var keyListener:Object = new Object();
keyListener.onkeydown = function() {
	if (Key.isDown(Key.RIGHT)) {
		circle_mc._rotation += 5;
	}
	if (Key.isDown(Key.LEFT)) {
		circle_mc._rotation -= 5;
	}
};
Key.addListener(keyListener);
this.onload = function() {
	var canvas:Object = new Object();
	canvas.width = 400;
	canvas.height = 300;
	this.createEmptyMovieClip("circle_mc", 1);
	circle_mc.lineStyle(0, 0x000000, 100);
	drawCircle(circle_mc, 0, 0, 25);
	drawCircle(circle_mc, 20, 0, 5);
	circle_mc._x = 100;
	circle_mc._y = 100;
};
function drawCircle(mc:MovieClip, x:Number, y:Number, r:Number):Void {
	mc.moveTo(x+r, y);
	mc.curveTo(r+x, Math.tan(Math.PI/8)*r+y, Math.sin(Math.PI/4)*r+x, Math.sin(Math.PI/4)*r+y);
	mc.curveTo(Math.tan(Math.PI/8)*r+x, r+y, x, r+y);
	mc.curveTo(-Math.tan(Math.PI/8)*r+x, r+y, -Math.sin(Math.PI/4)*r+x, Math.sin(Math.PI/4)*r+y);
	mc.curveTo(-r+x, Math.tan(Math.PI/8)*r+y, -r+x, y);
	mc.curveTo(-r+x, -Math.tan(Math.PI/8)*r+y, -Math.sin(Math.PI/4)*r+x, -Math.sin(Math.PI/4)*r+y);
	mc.curveTo(-Math.tan(Math.PI/8)*r+x, -r+y, x, -r+y);
	mc.curveTo(Math.tan(Math.PI/8)*r+x, -r+y, Math.sin(Math.PI/4)*r+x, -Math.sin(Math.PI/4)*r+y);
	mc.curveTo(r+x, -Math.tan(Math.PI/8)*r+y, r+x, y);
}

Was This Post Helpful? 0
  • +
  • -

#5 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Unofficial Flash Challenges 2

Posted 10 August 2008 - 08:43 PM

Challenge 2
Paste into Frame 1
this.onEnterFrame = function() {
	var opposite:Number = _root._ymouse-100;
	var adjacent:Number = _root._xmouse-100;
	var angle:Number = adjacent/(Math.sqrt(Math.pow(opposite, 2)+Math.pow(adjacent, 2)));
	angle = Math.acos(angle)*180/Math.PI;
	if (opposite<0) {
		angle *= -1;
	}
	circle_mc._rotation = angle;
};
this.onload = function() {
	var canvas:Object = new Object();
	canvas.width = 400;
	canvas.height = 300;
	this.createEmptyMovieClip("circle_mc", 1);
	circle_mc.lineStyle(0, 0x000000, 100);
	drawCircle(circle_mc, 0, 0, 25);
	drawCircle(circle_mc, 20, 0, 5);
	circle_mc._x = 100;
	circle_mc._y = 100;
};
function drawCircle(mc:MovieClip, x:Number, y:Number, r:Number):Void {
	mc.moveTo(x+r, y);
	mc.curveTo(r+x, Math.tan(Math.PI/8)*r+y, Math.sin(Math.PI/4)*r+x, Math.sin(Math.PI/4)*r+y);
	mc.curveTo(Math.tan(Math.PI/8)*r+x, r+y, x, r+y);
	mc.curveTo(-Math.tan(Math.PI/8)*r+x, r+y, -Math.sin(Math.PI/4)*r+x, Math.sin(Math.PI/4)*r+y);
	mc.curveTo(-r+x, Math.tan(Math.PI/8)*r+y, -r+x, y);
	mc.curveTo(-r+x, -Math.tan(Math.PI/8)*r+y, -Math.sin(Math.PI/4)*r+x, -Math.sin(Math.PI/4)*r+y);
	mc.curveTo(-Math.tan(Math.PI/8)*r+x, -r+y, x, -r+y);
	mc.curveTo(Math.tan(Math.PI/8)*r+x, -r+y, Math.sin(Math.PI/4)*r+x, -Math.sin(Math.PI/4)*r+y);
	mc.curveTo(r+x, -Math.tan(Math.PI/8)*r+y, r+x, y);
}

Was This Post Helpful? 0
  • +
  • -

#6 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Unofficial Flash Challenges 2

Posted 10 August 2008 - 08:58 PM

Challenge 3
Paste into Frame 1
var keyListener:Object = new Object();
keyListener.onkeydown = function() {
	if (Key.isDown(Key.RIGHT)) {
		square_mc._x += 5;
	}
	if (Key.isDown(Key.LEFT)) {
		square_mc._x -= 5;
	}
	if (Key.isDown(Key.UP)) {
		square_mc._y -= 5;
	}
	if (Key.isDown(KEY.DOWN)) {
		square_mc._y += 5;
	}
};
Key.addListener(keyListener);
this.onEnterFrame = function() {
	var opposite:Number = square_mc._y-100;
	var adjacent:Number = square_mc._x-100;
	var angle:Number = adjacent/(Math.sqrt(Math.pow(opposite, 2)+Math.pow(adjacent, 2)));
	angle = Math.acos(angle)*180/Math.PI;
	if (opposite<0) {
		angle *= -1;
	}
	circle_mc._rotation = angle;
};
this.onload = function() {
	var canvas:Object = new Object();
	canvas.width = 400;
	canvas.height = 300;
	this.createEmptyMovieClip("circle_mc", 1);
	circle_mc.lineStyle(0, 0x000000, 100);
	drawCircle(circle_mc, 0, 0, 25);
	drawCircle(circle_mc, 20, 0, 5);
	circle_mc._x = 100;
	circle_mc._y = 100;
	this.createEmptyMovieClip("square_mc", 2);
	square_mc.lineStyle(0, 0xff0000, 100);
	drawSquare(square_mc, 0, 0, 10);
	square_mc._x = 100;
	square_mc._y = 100;
};
function drawSquare(mc:MovieClip, x:Number, y:Number, s:Number):Void {
	mc.moveTo(x+s/2, y+s/2);
	mc.lineTo(x+s/2-s, y+s/2);
	mc.lineTo(x+s/2-s, y+s/2-s);
	mc.lineTo(x+s/2, y+s/2-s);
	mc.lineTo(x+s/2, y+s/2);
}
function drawCircle(mc:MovieClip, x:Number, y:Number, r:Number):Void {
	mc.moveTo(x+r, y);
	mc.curveTo(r+x, Math.tan(Math.PI/8)*r+y, Math.sin(Math.PI/4)*r+x, Math.sin(Math.PI/4)*r+y);
	mc.curveTo(Math.tan(Math.PI/8)*r+x, r+y, x, r+y);
	mc.curveTo(-Math.tan(Math.PI/8)*r+x, r+y, -Math.sin(Math.PI/4)*r+x, Math.sin(Math.PI/4)*r+y);
	mc.curveTo(-r+x, Math.tan(Math.PI/8)*r+y, -r+x, y);
	mc.curveTo(-r+x, -Math.tan(Math.PI/8)*r+y, -Math.sin(Math.PI/4)*r+x, -Math.sin(Math.PI/4)*r+y);
	mc.curveTo(-Math.tan(Math.PI/8)*r+x, -r+y, x, -r+y);
	mc.curveTo(Math.tan(Math.PI/8)*r+x, -r+y, Math.sin(Math.PI/4)*r+x, -Math.sin(Math.PI/4)*r+y);
	mc.curveTo(r+x, -Math.tan(Math.PI/8)*r+y, r+x, y);
}

Was This Post Helpful? 0
  • +
  • -

#7 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Unofficial Flash Challenges 2

Posted 10 August 2008 - 09:12 PM

Challenge 4
Paste into Frame 1 400px x 300px
var keyListener:Object = new Object();
keyListener.onkeydown = function() {
	if (Key.isDown(Key.RIGHT)) {
		square_mc._x += 5;
	}
	if (Key.isDown(Key.LEFT)) {
		square_mc._x -= 5;
	}
	if (Key.isDown(Key.UP)) {
		square_mc._y -= 5;
	}
	if (Key.isDown(KEY.DOWN)) {
		square_mc._y += 5;
	}
};
Key.addListener(keyListener);
this.onEnterFrame = function() {
	var opposite:Number = square_mc._y-circle_mc._y;
	var adjacent:Number = square_mc._x-circle_mc._x;
	var angle:Number = adjacent/(Math.sqrt(Math.pow(opposite, 2)+Math.pow(adjacent, 2)));
	angle = Math.acos(angle)*180/Math.PI;
	if (opposite<0) {
		angle *= -1;
	}
	circle_mc._rotation = angle;
	opposite = square_mc._y-duplicate_mc._y;
	adjacent = square_mc._x-duplicate_mc._x;
	angle = adjacent/(Math.sqrt(Math.pow(opposite, 2)+Math.pow(adjacent, 2)));
	angle = Math.acos(angle)*180/Math.PI;
	if (opposite<0) {
		angle *= -1;
	}
	duplicate_mc._rotation = angle;
	if (square_mc._y<294) {
		square_mc._y += 1;
	}
};
this.onload = function() {
	var canvas:Object = new Object();
	canvas.width = 400;
	canvas.height = 300;
	this.createEmptyMovieClip("circle_mc", 1);
	circle_mc.lineStyle(0, 0x000000, 100);
	drawCircle(circle_mc, 0, 0, 25);
	drawCircle(circle_mc, 20, 0, 5);
	var duplicate_mc:MovieClip = circle_mc.duplicateMovieClip("duplicate_mc", 2);
	circle_mc._x = 25;
	circle_mc._y = 25;
	duplicate_mc._x = canvas.width-25;
	duplicate_mc._y = 25;
	this.createEmptyMovieClip("square_mc", 3);
	square_mc.lineStyle(0, 0xff0000, 100);
	drawSquare(square_mc, 0, 0, 10);
	square_mc._x = 100;
	square_mc._y = 100;
};
function drawSquare(mc:MovieClip, x:Number, y:Number, s:Number):Void {
	mc.moveTo(x+s/2, y+s/2);
	mc.lineTo(x+s/2-s, y+s/2);
	mc.lineTo(x+s/2-s, y+s/2-s);
	mc.lineTo(x+s/2, y+s/2-s);
	mc.lineTo(x+s/2, y+s/2);
}
function drawCircle(mc:MovieClip, x:Number, y:Number, r:Number):Void {
	mc.moveTo(x+r, y);
	mc.curveTo(r+x, Math.tan(Math.PI/8)*r+y, Math.sin(Math.PI/4)*r+x, Math.sin(Math.PI/4)*r+y);
	mc.curveTo(Math.tan(Math.PI/8)*r+x, r+y, x, r+y);
	mc.curveTo(-Math.tan(Math.PI/8)*r+x, r+y, -Math.sin(Math.PI/4)*r+x, Math.sin(Math.PI/4)*r+y);
	mc.curveTo(-r+x, Math.tan(Math.PI/8)*r+y, -r+x, y);
	mc.curveTo(-r+x, -Math.tan(Math.PI/8)*r+y, -Math.sin(Math.PI/4)*r+x, -Math.sin(Math.PI/4)*r+y);
	mc.curveTo(-Math.tan(Math.PI/8)*r+x, -r+y, x, -r+y);
	mc.curveTo(Math.tan(Math.PI/8)*r+x, -r+y, Math.sin(Math.PI/4)*r+x, -Math.sin(Math.PI/4)*r+y);
	mc.curveTo(r+x, -Math.tan(Math.PI/8)*r+y, r+x, y);
}

Was This Post Helpful? 0
  • +
  • -

#8 eddieboy665  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 113
  • Joined: 09-April 07

Re: Unofficial Flash Challenges 2

Posted 11 August 2008 - 01:37 AM

Challenge 1:

Written in AS3, using Flex Builder.
For Flash: save an AS3 .fla in the same folder as these 2 classes, add 'FlashChallenges2_1' to "Document Class"

FlashChallenges2_1.as
package {
	
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	
	public class FlashChallenges2_1 extends Sprite 
	{
		
		private var twoCirs:TwoCircles; 
		
		public function FlashChallenges2_1() {
			
			initStage();
			
			twoCirs = new TwoCircles();
			this.addChild(twoCirs);
			twoCirs.x = (this.stage.stageWidth - twoCirs.width/2) / 2;
			twoCirs.y = (this.stage.stageHeight - twoCirs.height/2) / 2;
			
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
		
		}
		
		private function initStage():void {
			
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
        	this.stage.align = StageAlign.TOP_LEFT;

		}
		
		private function myKeyDown(e:KeyboardEvent):void {
			
			switch (e.keyCode) {
				
				case Keyboard.RIGHT:
					twoCirs.rotation += 5;
					break;
				case Keyboard.LEFT:
					twoCirs.rotation -= 5;
					break;
					
			}
			
		}
		
	}

}



TwoCircles.as
package {
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	
	public class TwoCircles extends Sprite 
	{
		
		private const LRG_CIR_RAD:uint = 25;
		private const SM_CIR_RAD:uint = 5;
		
		private var largeCircle:Sprite;
		private var smallCircle:Sprite;
		
		public function TwoCircles() {
			
			drawCircles();
		
		}
		
		private function drawCircles():void {
			
			largeCircle = new Sprite();
			smallCircle = new Sprite();
			
			largeCircle.graphics.beginFill(0xFF0000);
			largeCircle.graphics.drawCircle(0, 0, LRG_CIR_RAD); 
			largeCircle.graphics.endFill();
			
			smallCircle.graphics.beginFill(0x00FF00);
			smallCircle.graphics.drawCircle(LRG_CIR_RAD - SM_CIR_RAD, 0, SM_CIR_RAD);
			smallCircle.graphics.endFill();
			
			this.addChild(largeCircle);
			this.addChild(smallCircle);
			
		}
	
	}

}


Was This Post Helpful? 0
  • +
  • -

#9 eddieboy665  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 113
  • Joined: 09-April 07

Re: Unofficial Flash Challenges 2

Posted 11 August 2008 - 04:08 AM

Challenge 2:

Written in AS3, using Flex Builder.
For Flash: save an AS3 .fla in the same folder as this class and the 'TwoCircles' file, add 'FlashChallenges2_2' to "Document Class"
Requires TwoCircles.as (see above)

FlashChallenges2_2.as
package {
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.MouseEvent;
	
	public class FlashChallenges2_2 extends Sprite 
	{
		
		private var twoCirs:TwoCircles; 
		
		private var xDiff:int;
		private var yDiff:int;
		private var angle:int;
		
		public function FlashChallenges2_2() {
			
			initStage();
			
			twoCirs = new TwoCircles();
			this.addChild(twoCirs);
			twoCirs.x = (this.stage.stageWidth - twoCirs.width/2) / 2;
			twoCirs.y = (this.stage.stageHeight - twoCirs.height/2) / 2;
			
			this.stage.addEventListener(MouseEvent.MOUSE_MOVE, myMouseMove);
		
		}
		
		private function initStage():void {
			
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
        	this.stage.align = StageAlign.TOP_LEFT;

		}
		
		private function myMouseMove(e:MouseEvent):void {
			
			// Calculate angle of mouse coords from circles
			xDiff = e.stageX - twoCirs.x; // Adjacent
			yDiff = e.stageY - twoCirs.y; // Opposite
			/* 
			 * -----------------------------
			 *   O     |     A     |     O
			 * S   H   |   C   H   |   T   A
			 * -----------------------------
			 */
			angle = Math.atan(yDiff / xDiff) * (180 / Math.PI);
			if (xDiff < 0) angle += 180;
			// Set angle of rotation
			twoCirs.rotation = angle;
			
		}
		
	}

}


Was This Post Helpful? 0
  • +
  • -

#10 eddieboy665  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 113
  • Joined: 09-April 07

Re: Unofficial Flash Challenges 2

Posted 11 August 2008 - 04:55 AM

Challenge 3:

Written in AS3, using Flex Builder.
For Flash: save an AS3 .fla in the same folder as this class and the 'TwoCircles' file, add 'FlashChallenges2_3' to "Document Class"
Requires TwoCircles.as (see above)

FlashChallenges2_3.as
package {
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	public class FlashChallenges2_3 extends Sprite 
	{
		private const SQR_MOVE_AMOUNT = 5;
		private const SQR_SIZE = 20;
		
		private var twoCirs:TwoCircles; 
		
		private var square:Sprite;
		private var sqr_x:uint;
		private	var sqr_y:uint
		
		private var xDiff:int;
		private var yDiff:int;
		private var angle:int;
		
		public function FlashChallenges2_3() {
			
			initStage();
			initCircles();
			initSquare();
			
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
		
		}
		
		private function initStage():void {
			
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
        	this.stage.align = StageAlign.TOP_LEFT;

		}
		
		private function initSquare():void {
			
			sqr_x = (this.stage.stageWidth - SQR_SIZE) / 2;
			sqr_y = (this.stage.stageHeight - SQR_SIZE) / 2;
			 
			square = new Sprite();
			square.graphics.beginFill(0x0000FF, 0.8);
			square.graphics.drawRect(sqr_x, sqr_y, SQR_SIZE, SQR_SIZE);
			square.graphics.endFill();
			
			this.addChild(square);
				
		}
		
		private function initCircles():void {
			
			twoCirs = new TwoCircles();
			twoCirs.x = (this.stage.stageWidth - twoCirs.width/2) / 2;
			twoCirs.y = (this.stage.stageHeight - twoCirs.height/2) / 2;
			
			this.addChild(twoCirs);
			
		}
		
		private function myKeyDown(e:KeyboardEvent):void {
			
			var key:int = e.keyCode;
				
			if (key == Keyboard.UP)	square.y -= SQR_MOVE_AMOUNT;
			if (key == Keyboard.DOWN) square.y += SQR_MOVE_AMOUNT;
			if (key == Keyboard.RIGHT) square.x += SQR_MOVE_AMOUNT;
			if (key == Keyboard.LEFT) square.x -= SQR_MOVE_AMOUNT;
			
			rotateCircles();
			
		}
		
		private function rotateCircles():void {
			
			// Calculate angle of mouse coords from circles
			xDiff = square.x - twoCirs.x + sqr_x + SQR_SIZE/2; // Adjacent
			yDiff = square.y - twoCirs.y + sqr_y + SQR_SIZE/2; // Opposite
			/* 
			 * -----------------------------
			 *   O     |     A     |     O
			 * S   H   |   C   H   |   T   A
			 * -----------------------------
			 */
			angle = Math.atan(yDiff / xDiff) * (180 / Math.PI);
			if (xDiff < 0) angle += 180;
			
			// Set angle of rotation
			twoCirs.rotation = angle;
			
		}
		
	}

}


Was This Post Helpful? 0
  • +
  • -

#11 eddieboy665  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 113
  • Joined: 09-April 07

Re: Unofficial Flash Challenges 2

Posted 12 August 2008 - 01:23 AM

Challenge 4:

Written in AS3, using Flex Builder.
For Flash: save an AS3 .fla in the same folder as this class and the 'TwoCircles' file, add 'FlashChallenges2_4' to "Document Class"
Requires TwoCircles.as (see above)
Some of this is a bit dodgy!

FlashChallenges2_4.as
package {
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.ui.Keyboard;
	import flash.utils.Timer;
	
	public class FlashChallenges2_4 extends Sprite 
	{
		private const SQR_MOVE_AMOUNT:uint = 5;
		private const SQR_SIZE:uint = 20;
		private const ANIMATION_SPEED:uint = 50;
		private const GRAVITY:Number = 1.2;
		private const SQR_SPEED:Number = 1.2;
		private const DAMPING:Number = 0.5;
		private const BOUNCE_LEVEL:uint = 50;
		
		private var twoCirs_left:TwoCircles; 
		private var twoCirs_right:TwoCircles;
		
		private var timer:Timer;
		
		private var square:Sprite;
		private var sqr_max_x:uint;
		private var sqr_max_y:uint;
		private var sqr_speed:Number = SQR_SPEED;
		private var sqr_goingUp:Boolean = false;
		private var sqr_moving:Boolean = false;
		private var sqr_bounces:int = 0;
		
		private var down_isDown:Boolean = false;
		private var up_isDown:Boolean = false;
		private var left_isDown:Boolean = false;
		private var right_isDown:Boolean = false;
		
		private var xDiff:int;
		private var yDiff:int;
		private var angle:int;
		
		public function FlashChallenges2_4() {
			
			initStage();
			initCircles();
			initSquare();
			initTimer();
			
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
			this.stage.addEventListener(KeyboardEvent.KEY_UP, myKeyUp)
			
			rotateCircles();
		
		}
		
		private function initStage():void {
			
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
        	this.stage.align = StageAlign.TOP_LEFT;

		}
		
		private function initSquare():void {
			
			sqr_max_x = (this.stage.stageWidth - SQR_SIZE) / 2;
			sqr_max_y = (this.stage.stageHeight - SQR_SIZE) / 2;
			 
			square = new Sprite();
			square.graphics.beginFill(0x0000FF, 0.8);
			square.graphics.drawRect(sqr_max_x, sqr_max_y, SQR_SIZE, SQR_SIZE);
			square.graphics.endFill();
			
			square.y = sqr_max_y;
			
			this.addChild(square);
				
		}
		
		private function initCircles():void {
			
			twoCirs_left = new TwoCircles();
			twoCirs_left.x = twoCirs_left.width/2;
			twoCirs_left.y = twoCirs_left.height/2;
			this.addChild(twoCirs_left);
			
			twoCirs_right = new TwoCircles();
			twoCirs_right.x = this.stage.stageWidth - twoCirs_right.width/2;
			twoCirs_right.y = twoCirs_right.height/2;
			this.addChild(twoCirs_right);			
			
		}
		
		private function initTimer():void {
			
			timer = new Timer(ANIMATION_SPEED);
			timer.addEventListener(TimerEvent.TIMER, myOnTimer);
			timer.start();
			
		}
		
		private function myOnTimer(e:TimerEvent):void {
			
			if (up_isDown) {
				square.y -= SQR_MOVE_AMOUNT;
				sqr_speed = SQR_SPEED;
				sqr_moving = true;
				sqr_bounces = (sqr_max_y - square.y) / BOUNCE_LEVEL;
			}
			if (down_isDown) square.y += SQR_MOVE_AMOUNT;
			if (right_isDown) square.x += SQR_MOVE_AMOUNT;
			if (left_isDown) square.x -= SQR_MOVE_AMOUNT;
			
			if (!up_isDown && !sqr_goingUp && sqr_moving) {
							
				sqr_speed *= GRAVITY;
				square.y += sqr_speed;
				
				if (square.y > sqr_max_y) {
					sqr_bounces--;
					if (sqr_bounces >= 0) sqr_goingUp = true;
					else sqr_moving = false;	
				}
				
			} else if (sqr_goingUp) {

				sqr_speed *= DAMPING;
				square.y -= sqr_speed;
				
				if (sqr_speed < 0.3) {
					sqr_goingUp = false;
					sqr_speed = SQR_SPEED;
				}
				
			}
			
			stopSqrOfEdge();
			rotateCircles();
		}
		
		private function myKeyDown(e:KeyboardEvent):void {
		
			var key:int = e.keyCode;
				
			if (key == Keyboard.UP)	up_isDown = true;
			if (key == Keyboard.DOWN) down_isDown = true;
			if (key == Keyboard.RIGHT) right_isDown = true;
			if (key == Keyboard.LEFT) left_isDown = true;

		}
		
		private function myKeyUp(e:KeyboardEvent):void {
			
			var key:int = e.keyCode;
				
			if (key == Keyboard.UP)	up_isDown = false;
			if (key == Keyboard.DOWN) down_isDown = false;
			if (key == Keyboard.RIGHT) right_isDown = false;
			if (key == Keyboard.LEFT) left_isDown = false;

		}
		
		private function stopSqrOfEdge():void {
			
			if (square.y > sqr_max_y) square.y = sqr_max_y;
			if (square.y < -sqr_max_y) square.y = -sqr_max_y;
			if (square.x > sqr_max_x) square.x = sqr_max_x;
			if (square.x < -sqr_max_x) square.x = -sqr_max_x;
			
		}
		
		private function rotateCircles():void {
			
			twoCirs_left.rotation = getNewRotation(twoCirs_left);
			twoCirs_right.rotation = getNewRotation(twoCirs_right);
		
		}
		
		private function getNewRotation(twoCirs:TwoCircles):int {
			
			// Calculate angle of square coords from circles
			xDiff = square.x - twoCirs.x + sqr_max_x + SQR_SIZE/2; // Adjacent
			yDiff = square.y - twoCirs.y + sqr_max_y + SQR_SIZE/2; // Opposite
			/* 
			 * -----------------------------
			 *   O     |     A     |     O
			 * S   H   |   C   H   |   T   A
			 * -----------------------------
			 */
			angle = Math.atan(yDiff / xDiff) * (180 / Math.PI);
			if (xDiff < 0) angle += 180;
			
			return angle;
			
		}
		
	}

}


Was This Post Helpful? 0
  • +
  • -

#12 eddieboy665  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 113
  • Joined: 09-April 07

Re: Unofficial Flash Challenges 2

Posted 12 August 2008 - 09:55 AM

Challenge 5:

Written in AS3, using Flex Builder.
For Flash: save an AS3 .fla in the same folder as this class and the 'TwoCircles' file, add 'FlashChallenges2_5' to "Document Class"
Requires TwoCircles.as (see above)
N.B. Doesn't completely work, collisions are a bit temperamental, if the square gets far enough away from a magnet (and hence gains more speed) the collision is not recognised in time and once its overlapping the magnet, it won't let you move it. But I feel i've spent enough time on this one now, maybe someone wants to finish it off...

What I've done can be seen here.

FlashChallenges2_5.as
package {
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.ui.Keyboard;
	import flash.utils.Timer;
	
	public class FlashChallenges2_5 extends Sprite 
	{
		private const SQR_MOVE_AMOUNT:uint = 5;
		private const SQR_SIZE:uint = 20;
		private const ANIMATION_SPEED:uint = 50;
		private const CIR_LEFT_STRG:Number = 3.2;
		private const CIR_RIGHT_STRG:Number = 3.2;
		private const Y_PULL_REDUCTION:Number = 0.5;
		
		private var twoCirs_left:TwoCircles; 
		private var twoCirs_right:TwoCircles;
		
		private var timer:Timer;
		
		private var square:Sprite;
		private var sqr_max_x:uint;
		private var sqr_max_y:uint;
		private var sqr_speed_x:Number = 0;
		private var sqr_speed_y:Number = 0;
		private var sqr_stuck:Boolean = false;
		
		private var pull_top:Number;
		private var pull_left:Number;
		private var pull_right:Number;
		private var pullProportion_total_left:Number;
		private var pullProportion_total_right:Number;
		
		private var collisionDiff_x:int;
		private var collisionDiff_y:int;
		
		private var down_isDown:Boolean = false;
		private var up_isDown:Boolean = false;
		private var left_isDown:Boolean = false;
		private var right_isDown:Boolean = false;
		
		private var xDiff:int;
		private var yDiff:int;
		private var angle:int;
		
		public function FlashChallenges2_5() {
			
			initStage();
			initCircles();
			initSquare();
			initTimer();
			
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
			this.stage.addEventListener(KeyboardEvent.KEY_UP, myKeyUp)
			
			rotateCircles();
		
		}
		
		private function initStage():void {
			
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
        	this.stage.align = StageAlign.TOP_LEFT;

		}
		
		private function initSquare():void {
			
			sqr_max_x = (this.stage.stageWidth - SQR_SIZE) / 2;
			sqr_max_y = (this.stage.stageHeight - SQR_SIZE) / 2;
			 
			square = new Sprite();
			square.graphics.beginFill(0x0000FF, 0.8);
			square.graphics.drawRect(sqr_max_x, sqr_max_y, SQR_SIZE, SQR_SIZE);
			square.graphics.endFill();
			
			square.y = sqr_max_y;
			square.x = 0;
			
			this.addChild(square);
				
		}
		
		private function initCircles():void {
			
			twoCirs_left = new TwoCircles();
			twoCirs_left.x = twoCirs_left.width/2;
			twoCirs_left.y = twoCirs_left.height/2;
			this.addChild(twoCirs_left);
			
			twoCirs_right = new TwoCircles();
			twoCirs_right.x = this.stage.stageWidth - twoCirs_right.width/2;
			twoCirs_right.y = twoCirs_right.height/2;
			this.addChild(twoCirs_right);			
			
		}
		
		private function initTimer():void {
			
			timer = new Timer(ANIMATION_SPEED);
			timer.addEventListener(TimerEvent.TIMER, myOnTimer);
			timer.start();
			
		}
		
		private function myOnTimer(e:TimerEvent):void {
			
			handleMovementReqs();
			if (!sqr_stuck) handleMagnetPull();
			
			stopSqrOfEdge();
			
			rotateCircles();
		}
		
		private function myKeyDown(e:KeyboardEvent):void {
		
			var key:int = e.keyCode;
				
			if (key == Keyboard.UP)	up_isDown = true;
			if (key == Keyboard.DOWN) down_isDown = true;
			if (key == Keyboard.RIGHT && sqr_stuck != 1) right_isDown = true;
			if (key == Keyboard.LEFT && sqr_stuck != -1) left_isDown = true;

		}
		
		private function myKeyUp(e:KeyboardEvent):void {
			
			var key:int = e.keyCode;
			
			sqr_stuck = false;
				
			if (key == Keyboard.UP)	up_isDown = false;
			if (key == Keyboard.DOWN) down_isDown = false;
			if (key == Keyboard.RIGHT) right_isDown = false;
			if (key == Keyboard.LEFT) left_isDown = false;

		}
		
		private function handleMovementReqs():void {
			
			if (up_isDown) handleCollision("y", -SQR_MOVE_AMOUNT);
			if (down_isDown) handleCollision("y", SQR_MOVE_AMOUNT);
			if (right_isDown) handleCollision("x", SQR_MOVE_AMOUNT);
			if (left_isDown) handleCollision("x", -SQR_MOVE_AMOUNT);
		
		}
		
		private function handleMagnetPull():void {
			
			// Max distance away from a circle
			var greatestDist:int = Math.pow(Math.pow(this.stage.stageWidth, 2) + Math.pow(this.stage.stageWidth, 2), 0.5);
			// Calculate pull (from both sides) (sides of right angle)
			pull_left = (this.stage.stageWidth/2 - square.x - SQR_SIZE/2);
			pull_right = (this.stage.stageWidth/2 + square.x - SQR_SIZE/2);
			pull_top = (this.stage.stageHeight/2 - square.y - SQR_SIZE/2);
			// Using Pythagorous' theorem, calculate total proportions 
			pullProportion_total_left = Math.pow(Math.pow(pull_left, 2) + Math.pow(pull_top, 2), 0.5) / greatestDist;
			pullProportion_total_right = Math.pow(Math.pow(pull_right, 2) + Math.pow(pull_top, 2), 0.5) / greatestDist;
			
			// Combine forces from both magnets to calculate speed (X-AXIS)
			sqr_speed_x += - (pullProportion_total_left * CIR_LEFT_STRG) 		// Left force
						   + (pullProportion_total_right * CIR_RIGHT_STRG); 	// Right force
			// Make speed zero if user requests other direction		   
			if ((sqr_speed_x > 0 && left_isDown) || (sqr_speed_x < 0 && right_isDown)) sqr_speed_x = 0;
			// Add speed to x coord	
			handleCollision("x", sqr_speed_x);
			
			// Combine forces from both magnets to calculate speed (Y-AXIS)
			sqr_speed_y += - (pullProportion_total_left * CIR_LEFT_STRG) 		// Left force
						  - (pullProportion_total_right * CIR_RIGHT_STRG); 		// Right force
			// Make speed zero if user requests other direction	
			if (sqr_speed_y < 0 && down_isDown) sqr_speed_y = 0;
			// Add speed to y coord			   
			handleCollision("y", sqr_speed_y * Y_PULL_REDUCTION); 
			
		}
		
		private function handleCollision(axis:String, speedInc:Number):void {
			
			if (axis == "x") {
				var tmpX:int = square.x; 
				square.x += speedInc;
				if (square.hitTestObject(twoCirs_left) || square.hitTestObject(twoCirs_right)) {
					square.x = tmpX;
					sqr_stuck = true;
				}
			} else { 
				var tmpY:int = square.y; 
				square.y += speedInc;
				if (square.hitTestObject(twoCirs_left) || square.hitTestObject(twoCirs_right)) {
					square.y = tmpY;
					sqr_stuck = true;
				}
			}
			if (sqr_stuck) {
				sqr_speed_y = 0;
				sqr_speed_x = 0;
				trace("COLLISION! (x)");
			}
			
		}
		
		private function stopSqrOfEdge():void {
			
			if (square.y > sqr_max_y) square.y = sqr_max_y;
			if (square.y < -sqr_max_y) square.y = -sqr_max_y;
			if (square.x > sqr_max_x) square.x = sqr_max_x;
			if (square.x < -sqr_max_x) square.x = -sqr_max_x;
			
		}
		
		private function rotateCircles():void {
			
			twoCirs_left.rotation = getNewRotation(twoCirs_left);
			twoCirs_right.rotation = getNewRotation(twoCirs_right);
		
		}
		
		private function getNewRotation(twoCirs:TwoCircles):int {
			
			// Calculate angle of square coords from circles
			xDiff = square.x - twoCirs.x + sqr_max_x + SQR_SIZE/2; // Adjacent
			yDiff = square.y - twoCirs.y + sqr_max_y + SQR_SIZE/2; // Opposite
			/* 
			 * -----------------------------
			 *   O     |     A     |     O
			 * S   H   |   C   H   |   T   A
			 * -----------------------------
			 */
			angle = Math.atan(yDiff / xDiff) * (180 / Math.PI);
			if (xDiff < 0) angle += 180;
			
			return angle;
			
		}
		
	}

}


This post has been edited by eddieboy665: 12 August 2008 - 09:56 AM

Was This Post Helpful? 0
  • +
  • -

#13 eddieboy665  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 113
  • Joined: 09-April 07

Re: Unofficial Flash Challenges 2

Posted 12 August 2008 - 10:01 AM

View Posteddieboy665, on 12 Aug, 2008 - 09:55 AM, said:

collisions are a bit temperamental

Collisions are a very temperamental!
Was This Post Helpful? 0
  • +
  • -

#14 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Unofficial Flash Challenges 2

Posted 20 August 2008 - 03:40 PM

So what's going on with this thread Betawar?
Was This Post Helpful? 0
  • +
  • -

#15 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1134
  • View blog
  • Posts: 7,094
  • Joined: 07-September 06

Re: Unofficial Flash Challenges 2

Posted 20 August 2008 - 04:24 PM

Well, at this point I am waiting for someone to post an attempt at the Holy Shit level, but if nobody does that soon I will add another set of challenges.

Or do you think I should just go on with more challenges?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2