Labs

It works in my machine!

Archive for the ‘AS3’ Category

Random Points inside a Triangle

without comments

just copy and paste the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 
function getRandomPointInTriangle (A:Point,B:Point,C:Point):Point {
 
	//P = aA + bB + cC
	//@see http://www.cgafaq.info/wiki/Random_Point_In_Triangle
 
	var a:Number = Math.random();
	var b:Number = Math.random();
 
	if (a + b > 1) {
		a = 1-a;
		b = 1-b;
	}
 
	var c:Number = 1-a-b;
 
	var rndX:Number = (a*A.x)+(b*B.x)+(c*C.x);
	var rndY:Number = (a*A.y)+(b*B.y)+(c*C.y);
 
	return new Point(rndX,rndY);
}
 
 
var triangleHeight:uint = 150;
var triangleShape:Shape = new Shape();
triangleShape.graphics.lineStyle(1,0xff0000);
triangleShape.graphics.moveTo(triangleHeight/2, 5);
triangleShape.graphics.lineTo(triangleHeight, triangleHeight+5);
triangleShape.graphics.lineTo(0, triangleHeight+5);
triangleShape.graphics.lineTo(triangleHeight/2, 5);
addChild(triangleShape);
 
 
var p1:Point = new Point(triangleHeight/2, 5);
var p2:Point = new Point(triangleHeight, triangleHeight+5);
var p3:Point = new Point(0, triangleHeight+5);
 
//
//
//
//
 
var source:BitmapData = new BitmapData(triangleHeight,triangleHeight,true,0);
var bitmap:Bitmap = new Bitmap(source);
 
var sprite:Sprite = new Sprite();
sprite.addChild(bitmap);
addChild(sprite);
 
 
source.fillRect(source.rect,0);
source.lock();
 
for (var i:int = 0;i<50;i++) {
	var p:Point = getRandomPointInTriangle(p1,p2,p3);
	source.setPixel32(p.x,p.y,0xffff0000);
}
 
source.unlock();

Cheers!

Written by Raúl

May 5th, 2009 at 1:50 pm

Posted in AS3

Always on Top

with one comment

today i found my self trying to put an Sprite on the top of the display list
so here is the basic idea for solve that problem

1
2
3
4
5
6
7
8
9
10
11
 
function alwaysOnTop_handler(e:Event):void {
	//make this child always on top!
	parent.addChild(this);
}
 
function added_handler(e:Event):void {
	parent.addEventListener(Event.ADDED, alwaysOnTop_handler, false, 0, true);
}
 
addEventListener(Event.ADDED_TO_STAGE, added_handler, false, 0, true)

cheers!

Written by Raúl

April 21st, 2009 at 5:24 pm

Posted in AS3, General

Tagged with

Validating a String

without comments

well this is a handy code snippet to validate a string
if you want to avoid some bad words, this is for you:

1
2
3
4
5
6
7
8
9
10
11
12
 
function isValid(str:String):Boolean {
        var regExp:RegExp = /\b(badword|nastyWord)\b/i;
	var _isValid:Boolean = !regExp.test(str);
        return _isValid;
}
 
var _inValidMessage = "this is a example for a bad badword it's really nasty";
var _validMessage = "Hello Word!";
 
trace(isValid(_inValidMessage)) //false
trace(isValid(_validMessage))//true

you can add more words just separating them by a pipeline ( | ) in the Regular Expression

Cheers.

Written by Raúl

April 6th, 2009 at 4:27 pm

Posted in AS3, General

Tagged with

wowFlag [flarToolkit + pv3d + wowEngine]

with 7 comments


wowFlag from Wolf on Vimeo.

As promised on youtube: here it’s the source

( thx to saqoosha for the help on the flarToolkit issue of translating the flag fixed vertex… you can find his example of doing this on his site, but he maded it for away3d =) )

the code is very extended, so if you have any doubts post them here, we will try and answer all of them :)

Written by wolf

March 4th, 2009 at 3:27 pm

[ papervision3d ] Texture switch at runtime

with 2 comments

Here we have a quick example on how to change the texture of a primitive object in papervision3d…

At the end this is what you get:

The Flash plugin is required to view this object.

This is the Document Class, its pretty simple and the methods are self descriptive so i don’t think you’ll have any trouble using it :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package  
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.SimpleButton;
	import org.papervision3d.view.BasicView;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.materials.BitmapAssetMaterial;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;
	import org.papervision3d.render.BasicRenderEngine;
 
	/**
	* ...
	* @author Wolfito
	*/
	public class Main extends Sprite
	{
 
		private var scene:Scene3D;
		private var camera:Camera3D;
		private var viewport:Viewport3D;
		private var render_engine:BasicRenderEngine;
		private var my_plane:Plane;
		private var active_index:int = 0;
 
		public function Main() 
		{
			create3dEnviroment();
			buildPlane();
			setEvents();
			render3d();
		}
 
		private function create3dEnviroment():void
		{
			camera = new Camera3D();
			scene = new Scene3D();
			viewport = new Viewport3D(0, 0, true, true);
			addChild(viewport);
			render_engine = new BasicRenderEngine();
		}
 
		private function setEvents():void
		{
			//
			stage.addEventListener(MouseEvent.MOUSE_OVER, startRendering, false, 0, true);
			stage.addEventListener(Event.MOUSE_LEAVE, stopRendering, false, 0, true);
			//
                       // button placed on the stage:
			this.switchButton.addEventListener(MouseEvent.CLICK, switchTexture, false, 0, true);
		}
 
		private function startRendering(e:MouseEvent):void 
		{
			if (!this.hasEventListener(Event.ENTER_FRAME))
			{
				this.addEventListener(Event.ENTER_FRAME, render, false, 0, true);
			}
		}
 
		private function stopRendering(e:Event):void 
		{
			this.removeEventListener(Event.ENTER_FRAME, render);
		}
 
		private function buildPlane():void
		{
			var texture:BitmapAssetMaterial = new BitmapAssetMaterial("Pocoyo_0");
			texture.smooth = true;
			my_plane = new Plane(texture, 500, 500, 4, 4);
			scene.addChild(my_plane);
			camera.target = my_plane;
		}
 
		private function render(e:Event):void
		{
			render3d();
			moveCamera();
		}
 
		private function render3d():void
		{
			render_engine.renderScene(scene, camera, viewport);
		}
 
		private function moveCamera():void
		{
			camera.x = (this.stage.mouseX - (this.stage.stageWidth / 2)) * 2;
			camera.y = (this.stage.mouseY - (this.stage.stageHeight / 2)) * 2;
		}
 
		private function switchTexture(e:MouseEvent):void
		{
			var bitmapData:BitmapData;
			if (active_index == 0)
			{
                               //get bitmap linked from the library:
				bitmapData = new Pocoyo_1(283, 283);
				active_index = 1;
			}
			else
			{
                               //get bitmap linked from the library:
				bitmapData = new Pocoyo_0(283, 283);
				active_index = 0;
			}
			my_plane.material.bitmap.dispose();
			my_plane.material.bitmap = bitmapData;
			my_plane.material.updateBitmap();
		}
	}
 
}

you can download the source code too! =)

Written by wolf

November 20th, 2008 at 6:32 pm

Sending Variables in AS3 A.K.A. sendAndLoad in AS2

with 16 comments

in AS2 sending and receiving variables was easy, you could do it even with your eyes closed =D
but in AS3 is another story

remember AS2??

1
2
3
4
var lv:LoadVars = new LoadVars();
lv.username = "ruliman";
lv.password = "123456";
lv.sendAndLoad("somefile.php",lv,"POST");

nice isn’t??

in AS3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var variables:URLVariables = new URLVariables();
variables.username = "ruliman";
variables.password= "123456";
//
trace(variables.toString());
var request:URLRequest = new URLRequest("somefile.php");
request.method = URLRequestMethod.POST;
request.data = variables;
 
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
 
try{
	loader.load(request);
}
catch (error:Error) {
	trace("Unable to load URL");
}

until here you can say “well it isn’t too bad just a bunch of classes, but is better organized =) i love AS3 ”

but……..when you compile Flash tell you this!

Error: Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.
at Error$/throwError()
at flash.net::URLVariables/decode()
at flash.net::URLVariables$iinit()
at flash.net::URLLoader/flash.net:URLLoader::onComplete()

MUAHAHAAA!!!

the PHP code is simple

1
2
3
4
<?
$user= $_POST["username "];
$pass= $_POST["password"];
?>

so…………where the error is coming from??
i google it and found this answer, is very simple:
my php wasn’t returning something so, put an echo throwing some variable something like:

1
2
3
4
5
6
<?
$user= $_POST["username "];
$pass= $_POST["password"];
 
echo "result=success";
?>

and that’s it everything compiles normal, now i can go back to dinner =D

Written by Raúl

November 6th, 2008 at 8:43 pm

Posted in AS3

VerifyError: Error #1025 or Error from hell

with one comment

i was trying to read an XML with namespaces and then Flash throw me this weird error

VerifyError: Error #1025: An invalid register 3 was accessed

then we found that was beacuse of these lines (locally declared in a function)

1
2
var ns = ttXML.namespace();
default xml namespace = ns;

the error comes when you try to call some function after those lines in your code

the solution that we found was declare ns variable outside of the function
and then everything works fine =D

Written by Raúl

October 29th, 2008 at 7:44 pm

Posted in AS3

SoundItem Class

with one comment

well we start with this Sound Util, just embed the SoundTransform and SoundChannel in one single Class and the fade ability (uses Tween Lite)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.grupow.media 
{
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundLoaderContext;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
 
	import gs.TweenLite;
	import gs.easing.Linear;
 
	/**
	* ...
	* @author Raúl Uranga
	*/
	public class SoundItem extends Sound
	{
 
		private var _channel:SoundChannel
		private var _soundTransform:SoundTransform;
 
		public function SoundItem(stream:URLRequest = null,conext:SoundLoaderContext = null) {
 
			super(stream,conext);
 
			_soundTransform = new SoundTransform();
 
		}
 
		public function fade(volume:Number,ms:int = 1000,handler:Function = null):void
		{
			//
			if (volume < 0)
				volume = 0;
			//
			if (volume > 1)
				volume = 1;
			//	
			TweenLite.to(this, ms/1000, {volume:volume,ease:Linear.easeNone,onComplete:handler});
		}
 
		public override function play(startTime:Number = 0,loops:int = 0,sndTransform:SoundTransform = null):SoundChannel
		{
			if(sndTransform != null)
				_soundTransform = sndTransform; 
 
			_channel = super.play(startTime, loops, _soundTransform);
			return _channel;
		}
 
		public function stop():void
		{
			try {
				_channel.stop();
			}catch (e:*) {
				//trace("SoundItem Error: SoundChannel is not defined, you need to start playing");
			}
		}
 
 
		public function get volume():Number
		{ 
			return _soundTransform.volume;
		}
 
		public function set volume(value:Number):void 
		{
			_soundTransform.volume = value;
 
			try {
				_channel.soundTransform = _soundTransform;
			}catch (e:*) {
				//trace("SoundItem Error: SoundChannel is not defined, you need to start playing");
			}
		}
 
	}
}

code available at google code

cheers!

Written by Raúl

October 23rd, 2008 at 3:45 pm

Posted in AS3