as3 firing bullets at a target enemy ships

DEMO SWF

DOWNLOAD THE FLA FILE HERE

The code within this movie clip can be adapted very easily for firing bullets out of a cannon or a gun simply by moving the code in and out of the game loop. An example would be instead of causing the bullet to fire when the mouse is clicked, you could make the bullet fire when the space bar is pressed just by adding an event listener for that.

I’m definitely not very good at Math at all or doing anything complicated with numbers at all really so now that I’ve managed to figure out how to move an object across the screen to the mouse cursor, I’ve decided to make a blog post about it and let anyone else have a look at the code as well.


var ball:Ball;
var shouldBallMove:Boolean = false;

//holds the velocity to add
var ballXSpeed:Number = 0;
var ballYSpeed:Number = 0;

//ball speed multiplier
var speed:Number = 4;

//the distance
var ballTravelDistance:Number = 0;

addEventListener(Event.ENTER_FRAME, gameLoop);
stage.addEventListener(MouseEvent.CLICK, onClickHandler);

function gameLoop(e:Event):void
{
	//if the ballTravelDistance is greater than 0, it means the ball should move
	if(ballTravelDistance > 0)
	{
		//moves the ball
		ball.x += ballXSpeed;
		ball.y += ballYSpeed;

		//minus the distance travelled from the travel distance
		ballTravelDistance -= speed;
	} else
	{
		//reset the speed and ballTravelDistance to 0
		ballXSpeed = 0;
		ballYSpeed = 0;
		ballTravelDistance = 0;
	}

	//output the mouseX and the mouseY values on screen
	mouseX_txt.text = "Mouse x: " + mouseX;
	mouseY_txt.text = "Mouse y: " + mouseY;
	//output the ball.x and ball.y values on screen
	ballX_txt.text = "Ball x: " + ball.x;
	ballY_txt.text = "Ball y: " + ball.y;
}

function onClickHandler(e:Event):void
{
	//set the ball rotation to point towards the mouse cursor
	var dx:Number = mouseX - ball.x;
	var dy:Number = mouseY - ball.y;
	ball.rotation = Math.atan2(dy, dx) * 180 / Math.PI;

	//calculate the distance
	ballTravelDistance = getDistance(ball);

	//output the distance calculated
	distance_txt.text = "Distance calculated: " + ballTravelDistance.toString();

	//calculate the
	var amountX:Number = speed * Math.cos(ball.rotation * Math.PI / 180);
	var amountY:Number = speed * Math.sin(ball.rotation * Math.PI / 180);

	//set the ball speed (velocity)
	ballXSpeed = amountX;
	ballYSpeed = amountY;

	//output the velocityX and velocityY
	velocityX_txt.text = "Velocity X: " + ballXSpeed;
	velocityY_txt.text = "Velicity Y: " + ballYSpeed;
}
function getDistance(object1:DisplayObject)
{
	//the delta's
	var dx:Number = mouseX - object1.x;
	var dy:Number = mouseY - object1.y;

	//calculate the distance
	var distance:Number = Math.sqrt(dx * dx + dy * dy);

	return distance;
}

How to very easily create a restful API using php

Here is an excellent article written by ‘Gen X Design’ that shows how to quickly implement a restful API using PHP.

http://www.gen-x-design.com/archives/create-a-rest-api-with-php/

How to resize images proportionally with actionscript 3 as3

It’s definitely something that every Actionscript 3 developer will want to know how to do at some point. This is just a brief explanation on one of the many ways to resize images proportionally. I like it because it makes use of the built in scaleX and scaleY properties which is definitely something that you want to take advantage of and it’s extremely simple.


//let's assume you want to contain your image at 100 pixels height.
image.height = 100;

//let's scale the width to match it perfectly proportionally
image.scaleX = image.scaleY;

Working with MySQL DateTime and PHP

This is a brief explanation of how to work with MySQL DateTime and PHP togather. It’s easy to get a php timestamp using the time() function in php and with the following code, you can then convert it easily to MySQL DateTime format so you can insert it into your MySQL database.


//gets the current time as an integer
$php_timestamp = time();

//formats the time according to MySQL DateTime and stores it
$mysql_formatted_time = date('Y-m-d H:i:s', $php_timestamp);

Ok, so now you can have the time that your user signed up for your application(using my imagination here) and insert that into your database. What if you found out the actual timezone of your user through other means such as asking them and you’d now like to convert the time to something else. Lets say for example you found out that the users timezone is UTC -5 and that you’d want to separate the time so you can get access to strings that says “April 10th, 2010″ and another that says “6:38 PM” (with no leading zero’s).


$t = strtotime('2010-04-30 11:38:56');
$t = mktime(date('G',$t)-5,date('i',$t),date('s',$t),date('n',$t),date('j',$t),date('Y',$t));
$t1 = date('F jS, Y',$t);
$t2 = date('h:i A',$t);

echo $t1 , $t2;

This code works even if you modify the above code so that the -5 becomes, -6, -13 or whatever you like, even positive numbers, +5, +20 even!.

This code is compliments an awesome PHP master named Sergey Popov, if you want to, you can find him around no problems, he’s awesome!

PHP file_get_contents v.s. PHP fopen v.s. PHP curl speed and performance

What is the fastest way to download the contents of a webpage with PHP?

Php fopen v.s. file_get_contents v.s. php curl speed

Ok, many people already know the answer to this question. In my performance benchmark that I ran today, curl came out on top as the clear winner. Even though it required more code to write, it came out on top as the winner. In my test, I contacted a web service using all three methods and PHP curl won the race by nearly half the time as it takes file_get_contents or fopen to download the contents of a webpage. In my testing, I used a flash application to contact my web servers php file and in the PHP code, I used all three of these methods for contacting the same web service.

It’s important to note that in my testing, I started in flash with a request to the PHP file and specified whether I wanted to run a curl request, a fopen request or a file_get_contents request. Each one of those requests contacts the php file, specified which test to run, contacted the webservice and downloaded the response and sent it back to flash. That whole process counted as 1 iteration and I did the test many times to be sure of my findings.

I will update this post at a later date with what operating system I used, what version of PHP, what version of flash and also post the PHP code that I used. Depending on if anyone actually wants to see these details that is. :)

UPDATE June 19th, 2010: Ok some people want to know the details of my performance test. I used each of these three methods to connect to a third party web service and receive a response. Each test consisted of the same web service request being performed exactly 15 times. As you can see from the scores below, CURL was the winner(Lower times is better). Each request was performed in the exact same way and just in case there is any doubt in your mind, I performed the test 4 times and below are my results measured in milliseconds.

TEST 1 Results
curl: 7564 fopen: 10599 file_get_contents: 9638

TEST 2 Results
curl: 7910 fopen: 9747 file_get_contents: 9663

TEST 3 Results
curl: 7804 fopen: 10518 file_get_contents: 12384

TEST 4 Results
curl: 6850 fopen: 9596 file_get_contents: 9557

How to get started using the new Facebook Graph API

The new Facebook Graph API is available for us immediately and some of you want to know how to get started. Along with the new Graph API, all of the client libraries have been updated (Javascript library, PHP library, etc).  This blog post is another bookmark post that serves to link to an already excellent tutorial on Integrating Facebook into your website or Facebook Application.

How to integrate Facebook into your website and use the Graph API

How to use the new PHP SDK in your facebook Application

Keep in mind there are two PHP Libraries now, the new facebook PHP SDK and the old PHP client library(This is the version that the Facebook wiki links to whenever they talk about the old php client library). For this old libary, if you browse http://pearhub.org/get/, you can see other newer versions are available there for download as well.

How to Apache .htaccess password protect directories the easy way

This is a another bookmark blog post that points to an awesome tutorial on how to password protect your web servers directories when your using the Apache web server. All of this is done through the .htaccess file so you only have to make sure that your Apache config file is set to AllowOverride All or so. Once that is done, you can then set your .htaccess password the easy way. :)

http://www.elated.com/articles/password-protecting-your-pages-with-htaccess/

How to move your mysql databases to another mount on your linux filesystem

If for some reason you want to move your MySQL database to another mount on your file system you can do it easily in fewer steps that you might have imagined.

1. From the MySQL CLI, run show variables like ‘datadir’ in order to check what your datadir is currently set to. Note it down somewhere.

2. Shutdown your MySQL so it’s no longer running at all. /sbin/service mysqld stop

3. Open up your my.cnf (MySQL config file) and under [mysqld] section, add(or change if it’s already existing) this line, datadir=/your/new/path

Note: If there any other paths in your my.cnf file that reference the old datadir, update the paths to the new path while your in there.

4. You can now tar up the contents and subdirectories of the old datadir and then extract it all to the new output directory.

5. Reboot your server

Note: Some people cannot login to their mysql command line interface anymore after doing this and so for those of you that have this trouble, you can add the following lines to your my.cnf file to fix the issue. Reboot your server afterwards.

[mysqld]

socket = /path/to_your/mysql.sock

[client]

socket = /path/to_your/mysql.sock

How to check, view and change the value of your servers mysql max_connections value

Many people want to know how to view or change the value of MySQL max_connections value on your database server(or web server, if they are the same). I find it very useful to refer to this post anytime I need to make the changes.

3 easy steps to check what the value of mysql max_connections is and to change it

STEP 1. Lets first login to ssh on your server and login to the mysql command line interface.

mysql -u root -p

Login the above command just starts up mysql CLI and your specifying that you want to login with a specific user with the -u switch followed the root which is the account you want to login with. The -p switch just causes it to prompt you for the password which you should know because you better have set it up after setting up your server. [Help located here if you didn't setup your mysql root password, or worse, if you need to recover it]

STEP 2. Once your successfully logged into the mysql CLI as the root user, type the below command to view what the current value of max_connections is set to on your server.

show variables like ‘max_connections’;

STEP 3. Edit the mysql config file on your server and then do a restart of the mysql service. After that, you can log back into the  mysql CLI to verify if the new value has been updated successfully.

The mysql config file is call my.cnf and its location varies but is usually located in /etc/my.cnf

If your locate is updated(by typing updatedb), you can try using ‘locate my.cnf’ command to try to find it’s location. Once you find it’s location, open it up in your favorite editor and add the following line under the [mysqld] section.

max_connections=500

Note: this file doesn’t need the semicolon at the end after the value but make sure to save the file after your done editing it and either restart the mysql service or else reboot your server(last resort only if restarting the service doesn’t work).

Forget phpmyadmin running on your Amazon ec2 instance, use MySQL Administrator instead

Do you want to install phpmyadmin on your Amazon ec2 instance and try to get connected to it to do all of your mysql administration? This is a problem for Amazon ec2 users and it’s well known that all Amazon ec2 instances that come with PHP and MySQL do not come with phpmyadmin. You can search for ‘how to install phpmyadmin on Amazon ec2′ or you can simply use MySQL Administrator right from your windows workstation, it’s fast and it’s easy.

So I now bring new light to an old HowToForge article by reviving it here today for you. This article explains exactly how you can connect to your MySQL on your Amazon ec2 instance using nothing other than our friend Putty. Although the article doesn’t mention Amazon ec2, I tested it and it works great.

source[http://www.howtoforge.com/secure_mysql_connection_ssh_tunnel]

Copyright Ricky does it, Game development blog