Monday, July 28, 2008

[AS3]Random numbers

Here is a quick AS3 random number explanation. In AS3, the Math.Random generates a random number between 0 and 1. So with that, we can get a random number between 0 and any number. Here is an example. Lets say we want a random number between 0 and 10. First we would need a dynamic textbox. Give it the instance name "displayRand". This would be our code:
displayRand.text = Math.floor(Math.random() * 10) + " ";

This will now generate a random number. You can change the number after Math.random to anything depending on the range that you want. Post or email me any questions.

Sunday, July 27, 2008

[AS3] Basic collision detection

Here is a basic method of collision detection. First lets make 2 squares, make them both movieclips, and give he instance names square1 and square2. Now lets make it so that you can drag them around the stage, to make them overlap. Here is some basic code to do this:
var drag:Boolean = false;
square1.addEventListener(MouseEvent.MOUSE_DOWN, mousedown);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseup);
stage.addEventListener(Event.ENTER_FRAME, enterframe);
function mousedown(e:MouseEvent):void{
drag = true;
}
function mouseup(e:MouseEvent):void{
drag = false;
}
function enterframe(e:Event):void{
if(drag == true){
square1.startDrag();
}
if(drag == false){
square1.stopDrag();
}
}

Now make a text box and make it dynamic and give it the instance name hittest.Now enter this code in the ENTER_FRAME function:
if(square1.hitTestObject(square2)){
hittest.text = "hitTest = true";
}
else{
hittest.text = "hitTest = false";
}

Post any questions.

[AS3] making a moving movieclip wrap the screen

Hey guys, here is a quick addition to the code I gave you guys yesterday on AS3 arrow key movement. So today Im gonna show you guys how to make the object you are controlling wrap the stage. So here is the code I showed you yesterday:

    import flash.events.*;
var rightArrow:Boolean;
var leftArrow:Boolean;
var upArrow:Boolean;
var downArrow:Boolean;
var speed:uint = 10;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.ENTER_FRAME, moveClip);
function keyDownHandler(event:KeyboardEvent):void{
if (event.keyCode == Keyboard.RIGHT) {
rightArrow = true;
}
if (event.keyCode == Keyboard.LEFT) {
leftArrow = true;
}
if (event.keyCode == Keyboard.UP) {
upArrow = true;
}
if (event.keyCode == Keyboard.DOWN) {
downArrow = true;
}
}
private function keyUpHandler(event:KeyboardEvent) {
if (event.keyCode == Keyboard.RIGHT) {
rightArrow = false;
}
if (event.keyCode == Keyboard.LEFT) {
leftArrow = false;
}
if (event.keyCode == Keyboard.UP) {
upArrow = false;
}
if (event.keyCode == Keyboard.DOWN) {
downArrow = false;
}
}
function enterFrameHandler(event:Event):void{
if (rightArrow) {
_clip.x += speed;
}
if (leftArrow) {
_clip.x -= speed;
}
if (upArrow) {
_clip.y -= speed;
}
if (downArrow) {
_clip.y += speed;
}
}



Now we are going to make it so that when it goes off the right side of the stage, it will appear on the left. In the enter frame function, within the right arrow if stetment, put this code:
if(object_mc.x > stage.stageWidth){
object_mc.x = -(object_mc.width);
}

This makes it so that once the movieclip's x coordinate is off of the stage, It appears just off the left side of the screen. Now here is the enterframe function again with the wrap stage code inserted:

   import flash.events.*;
var rightArrow:Boolean;
var leftArrow:Boolean;
var upArrow:Boolean;
var downArrow:Boolean;
var speed:uint = 10;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.ENTER_FRAME, moveClip);
function keyPressed(event:KeyboardEvent) {
if (event.keyCode == Keyboard.RIGHT) {
rightArrow = true;
}
if (event.keyCode == Keyboard.LEFT) {
leftArrow = true;
}
if (event.keyCode == Keyboard.UP) {
upArrow = true;
}
if (event.keyCode == Keyboard.DOWN) {
downArrow = true;
}
}
function keyReleased(event:KeyboardEvent) {
if (event.keyCode == Keyboard.RIGHT) {
rightArrow = false;
}
if (event.keyCode == Keyboard.LEFT) {
leftArrow = false;
}
if (event.keyCode == Keyboard.UP) {
upArrow = false;
}
if (event.keyCode == Keyboard.DOWN) {
downArrow = false;
}
}
function moveClip(event:Event) {
if (rightArrow) {
object_mc.x += speed;
if (object_mc.x > stage.stageWidth) {
object_mc.x = -(object_mc.width);
}
}
if (leftArrow) {
object_mc.x -= speed;
if (object_mc.x < -(object_mc.width)) { object_mc.x = stage.stageWidth; } } if (upArrow) { object_mc.y -= speed; if (object_mc.y < -(object_mc.height)) { object_mc.y = stage.stageHeight; } } if (downArrow) { object_mc.y += speed; if (object_mc.y > stage.stageHeight) {
object_mc.y = -(object_mc.height);
}
}
}


This code all works basically the same as the first example. Post or email me any questions.

Saturday, July 26, 2008

[C] WriteProcessMemory

What does write process memory do? Here is the definition from MSDN:
Writes data to an area of memory in a specified process. The entire area to be written to must be accessible or the operation fails.
And here are the parameters:
BOOL WINAPI WriteProcessMemory(
__in HANDLE hProcess,
__in LPVOID lpBaseAddress,
__in LPCVOID lpBuffer,
__in SIZE_T nSize,
__out SIZE_T *lpNumberOfBytesWritten
);
And defining each parameter:

hProcess [in]

A handle to the process memory to be modified. The handle must have PROCESS_VM_WRITE and PROCESS_VM_OPERATION access to the process.

lpBaseAddress [in]

A pointer to the base address in the specified process to which data is written. Before data transfer occurs, the system verifies that all data in the base address and memory of the specified size is accessible for write access, and if it is not accessible, the function fails.

lpBuffer [in]

A pointer to the buffer that contains data to be written in the address space of the specified process.

nSize [in]

The number of bytes to be written to the specified process.

lpNumberOfBytesWritten [out]

A pointer to a variable that receives the number of bytes transferred into the specified process. This parameter is optional. If lpNumberOfBytesWritten is NULL, the parameter is ignored.



So with that out of the way, I will show you a code snippit to writing memory to minesweeper.

#include "stdio.h"
#include "windows.h"
#include "tlhelp32.h"

int GetPID(LPCSTR ProcessName) //Function for getting the PID (Process ID)
{
HANDLE hProcessSnap;
// Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);

PROCESSENTRY32 pe32;
pe32.cntUsage = 1;
pe32.th32ModuleID = 0;
pe32.th32ParentProcessID = 0;
pe32.dwSize = sizeof(PROCESSENTRY32);

do
{
if (!strcmp(pe32.szExeFile, ProcessName))
{
return pe32.th32ProcessID;
}

}while (Process32Next(hProcessSnap, &pe32));

CloseHandle(hProcessSnap);
return 0;

}

BOOL IsActive( LPCSTR ProcName ) //Function for check if it is running
{
HANDLE hProcSnap;
hProcSnap = CreateToolhelp32Snapshot( TH32CS_SNAPALL, 0 );

PROCESSENTRY32 pe32;

do
{
if( !strcmp( pe32.szExeFile, ProcName ))
{
return TRUE;
}
}while( Process32Next( hProcSnap, &pe32 ));

CloseHandle( hProcSnap );
return 0;
}


int _tmain(int argc, _TCHAR* argv[])
{
if( IsActive( "winmine.exe" )) //Check if its running
{
HANDLE hProc; //Handle to Process
unsigned long PID; //Variable to store the PID
BYTE nop = { 0x90 }; //Buffer to write

PID = GetPID( "winmine.exe" ); //Obtains PID
hProc = OpenProcess( PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_CREATE_THREAD, FALSE, PID ); //Opens process to get the handle

//Change of 01002ff5 - ff 05 9c 57 00 01 - inc [0100579c]
if( WriteProcessMemory( hProc, ( LPVOID )0x01002ff5, &nop, sizeof( nop ), 0 )) //Changing the opcode to 0x90 (hex for nop)
{
printf( "Time Successfully frozen!\n" );
}
else
{
puts( "Could not write bytes for flags" );
}

getchar();
}
else
{
puts( "MineSweeper not found!" );
getchar();
}
return 0;
}



Questions? Just post or email me.

[AS3]Arrow key movement

Here is a quick tut on how to make a movieclip movable using the arrow keys.
First we are gonna need to import some events:
import flash.events.*;

Now lets declare a variable for speed:
var speed:Number = 5;

Now a few variables which will tell us whether or not each button is pressed:
var right:Boolean = false;
var left:Boolean = false;
var up:Boolean = false;
var down:Boolean = false;

now we are going to add an event listener that listens for any key being pressed, one that checks for keys released and one for enter frame:
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);

Now you need to create the movie clip that we are going to make move. It can be any shape or size. Give it the instance name object_mc . Now lets see the key down function. Type this:
function keyDownHandler(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.RIGHT){
right = true;
}
if(event.keyCode == Keyboard.LEFT){
left = true;
}
if(event.keyCode == Keyboard.UP){
up = true;
}
if(event.keyCode == Keyboard.DOWN){
down = true;
}
}

This basically makes it so that when one of the variables is true, that button was pressed.
Now for the key up function :
function keyUpHandler(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.RIGHT){
right = false;
}if(e.keyCode == Keyboard.LEFT){
left = false;
}if(e.keyCode == Keyboard.UP){
up = false;
}if(e.keyCode == Keyboard.DOWN){
down = false;
}
}

This makes it so that when the key is released, the variable returns to false. Without this, after pressing the button once, the movieclip would keep moving in that direction even after the key was released. Now make a movieclip. It can be any size or shape. Just give it the instance name "object_mc" without the quotes. Now for the enter frame function:
function enterFrameHandler(e:Event):void{
if(right){
object_mc.x += speed;
}if(left){
object_mc.x -= speed;
}if(up){
object_mc.y -= speed;
}if(down){
object_mc.y += speed;
}
}

This actually moves the movieclip while the key is held. You can make it move faster or slower by changing the speed variable. So, as you can see arrow key movement is pretty easy with AS3. Any questions, email me or post.

Friday, July 25, 2008

[C] Checking if a Process is active

I've seen lots of questions posted around many forums asking how to check if a process is running. And just to show you how easy it is, here is the code snippit.

#include "tlhelp32.h"
BOOL IsActive( LPCSTR ProcName )
{
HANDLE hProcessSnap; //Handle to store the snapshot in
//Take a snapshot of all running processes
hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPALL, 0 );

PROCESSENTRY32 pe32; //Structure for an entry for a process from the snapshot

do
{
if( !strcmp( pe32.szExeFile, ProcName ))
{
return TRUE; //Tells us it is active
}
}while( Process32Next( hProcessSnap, &pe32 )); //Keep searching each process

CloseHandle( hProcessSnap ); //Close current snapshot handle
return 0;
}

See? Not very hard is it! The usage would just be something like:

if( IsActive( "process.exe" ))
{
//code
}

[AS3] Gravity

Gravity is far simpler than it appears in AS3. However you have to understand a few key points:
  • As the object moves up on the stage, the Y coordinate decreases.
  • Therefore, if the y coordinate increases, it will be moving down.
With this in mind we first need to declare a few variables.
var yvel:Number = 10; /*how much the y coordinate will be increasing by when the frame is entered */
var gravity:Number = 1; //how much the yvel is going to be increased by
Now for our code:
import flash.events.* ;
stage.addEventListener(Event.ENTER_FRAME, enterFrameEventHandler); // stage event listener
//our function
function enterFrameEventHandler(e:Event):void{
my_mc.y += yvel; //makes the movie clip move
yvel += gravity; //gravity comes into play
}

Welcome

Well, me and Q got a blog up. Its going to be mainly on programming and junk.
Hope you like it.