Jump to content
N-Europe

FLASH help


nightwolf

Recommended Posts

Anyway I've been making a basic platformer in flash (ACTIONSCRIPT 3) for uni and I've managed to get the character moving and a scrolling background. Some form of retarded jump is in there too.

 

However, I need to be able to get character to pick up items maybe keep score of them, but I can't figure out how!

 

Any flash buffs help? :(

 

Current code:

 

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);

 

var buildingSpeed:Number=0;

 

 

buildings1.addEventListener(Event.ENTER_FRAME, moveBuilding);

buildings2.addEventListener(Event.ENTER_FRAME, moveBuilding);

 

 

 

 

function moveBuilding(evt:Event) {

 

evt.currentTarget.x+=buildingSpeed;

if (evt.currentTarget.x < -evt.currentTarget.width)

evt.currentTarget.x+= 2*evt.currentTarget.width

else if (evt.currentTarget.x > stage.stageWidth)

evt.currentTarget.x -= 2*evt.currentTarget.width

 

}

 

function keyDownHandler(key:KeyboardEvent) {

var temp=key.keyCode;

if (temp==Keyboard.RIGHT) {

buildingSpeed=-10;

pac_mc.scaleX = 0.5;

}

else if (temp == Keyboard.LEFT) {

buildingSpeed=10;

pac_mc.scaleX = -0.5;

}

else if (temp == Keyboard.UP) {

if(pac_mc.y > 185) pac_mc.y -= 15;}

else if (temp == Keyboard.DOWN) {

if (pac_mc.y < 360) pac_mc.y += 15;}

 

}

 

function keyUpHandler(key:KeyboardEvent) {

var temp=key.keyCode;

if (temp==Keyboard.RIGHT) {

buildingSpeed=0;

} else if (temp == Keyboard.LEFT) {

buildingSpeed=0;}

 

 

}

 

 

 

:(

Link to comment
Share on other sites

Well, again, sorry I can't actually help with code, but I've done plenty of C# programming so the way I'd approach it:

 

1. Get your power-ups on screen.

2. Create a power-up value and get it displayed on screen.

3. Test out the value works by increasing it every time you press a key on the keyboard.

4. Create collision code for your character and power-ups.

5. Detect that collision with a simple bool.

6. Replace the bool with the increased power-up code used before.

7. Create code to then make the power-up disappear.

 

I assume that this is 2D and that ActionScript and create simple bounding boxes for them based on their images; making the collision code a simple "intersects".

Link to comment
Share on other sites

addEventListener (Event.ENTER_FRAME, itemPickupAndDisplay);

var item:Number = 0;

function itemPickupAndDisplay (event:Event)
{
   if (pac_mc.hitTestObject(item_mc)) {
       item++;
       removeChild(item_mc); //This line should remove the item from the stage, really not sure if this line will work or not.
       item_txt.text = String ("Items:"+item);
   }
}

 

 

item_mc is instance name of the item you want to pick up. item_txt is the instance name of the dynamic text box you want to display the value in.

 

 

Hopefully this works. I'll admit I don't know AS3, I'm just putting this together from what I know about AS2, Java, and from what I've read in the AS3 documentation and a few tutorials.

 

On a side note, you should put your code in [php*][/php*] (without the stars) tags so that you keep the indentation, and get syntax highlighting, when positing it on forums.

Link to comment
Share on other sites

Right I managed to figure some of it out. Which is very similar to yours.

 

Unfortunetly now it just looks like my character moves WITH food_mc (the item pickup) instead of it moving towards the character to pick it up. Having this scrolling background is a pain.

 

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);


var buildingSpeed:Number=0;
var score:Number=0


buildings1.addEventListener(Event.ENTER_FRAME, moveBuilding);
buildings2.addEventListener(Event.ENTER_FRAME, moveBuilding);



function hitCheck(event:Event):void {

	if (pac_mc.hitTestObject(food)) {
		score += 1
		food.visible =false;			

}}


function moveBuilding(evt:Event) {

evt.currentTarget.x+=buildingSpeed;
if (evt.currentTarget.x < -evt.currentTarget.width) 
 evt.currentTarget.x+= 2*evt.currentTarget.width
 else if (evt.currentTarget.x > stage.stageWidth) 
 evt.currentTarget.x -= 2*evt.currentTarget.width

}

function keyDownHandler(key:KeyboardEvent) {
var temp=key.keyCode;
if (temp==Keyboard.RIGHT) {
	buildingSpeed=-10;
	pac_mc.scaleX = 0.5;
	} 
else if (temp == Keyboard.LEFT) {
	buildingSpeed=10;
	pac_mc.scaleX = -0.5;
	}
else if (temp == Keyboard.UP) {
	if(pac_mc.y > 185) pac_mc.y -= 15;}
   else if (temp == Keyboard.DOWN) {
   if (pac_mc.y < 360) pac_mc.y += 15;}

}

function keyUpHandler(key:KeyboardEvent) {
var temp=key.keyCode;
if (temp==Keyboard.RIGHT) {
	buildingSpeed=0;
} else if (temp == Keyboard.LEFT) {
	buildingSpeed=0;}


}

 

Fixed it.

 

Needed to add an eventlistener for the food_mc. Woot.

Edited by nightwolf
Link to comment
Share on other sites

×
×
  • Create New...