ForumsProgramming ForumMidmenj needs YOUR help!

6 3380
Midmenj
offline
Midmenj
216 posts
Nomad

Hello people and or aliens of Armor Games!

I have a few codes i would like to go over with you guys, just to see if its right or not... here they are:

Cosmetic background code:


onClipEvent(enterFrame){
this._x = _root.BG._x;
this._y = _root.BG._y;

Drag Code: insert this code into the object that is to be dragged.

Note: All draggable objects must be made on a second Layer.


on(press){
startdrag(""
}
on(release){
stopDrag();
}


Enemy code. Note: for this to work, the main character must have the instance name &quotlayer".



onClipEvent (enterFrame) { if (_root.player._x > _x) { _x += 5; }}onClipEvent (enterFrame) { if (_root.player._x<_x) { _x -= 5; }}onClipEvent (enterFrame) { if (_root.player._y>_y) { _y += 5; }}onClipEvent (enterFrame) { if (_root.player._y<_y) { _y -= 5; }}

// Initializing Variables
var MAX_HEALTH:Number = 25;
var charHealth:Number = MAX_HEALTH;
var enemyDamage:Number = 5;
var healthIncrement:Number = 10;

// Check hit and apply damage
if (_root.character.hitTest(_root.enemy)) {
if (charHealth > 0){
charHealth -= enemyDamage;
} else {

// Code for whatever happens when the character dies.

}
}

// If you want to cure the character (after all that's the only purpose I see for having a maxHealth)
if (_root.character.hitTest(_root.magicalCurePill)) {
charHealth += healthIncrement;
if (charHealth > MAX_HEALTH) charHealth = MAX_HEALTH;
}


onClipEvent (enterFrame) {
if (Key.isDown(Key.DOWN)) {
if (_root.BG.hitTest(_x, _y+5, true)) {
this._y += 4;
} else {
}
this.gotoAndStop(2);
}
if (Key.isDown(Key.UP)) {
if (_root.BG.hitTest(_x, _y-5, true)) {
this._y -= 4;
} else {
}
this.gotoAndStop(1);
}
if (Key.isDown(Key.LEFT)) {
if (_root.BG.hitTest(_x-5, _y, true)) {
this._x -= 4;
} else {
this.gotoAndStop(3);
}
}
if (Key.isDown(Key.RIGHT)) {
if (_root.BG.hitTest(_x+5, _y, true)) {
this._x += 4;
} else {
}
this.gotoAndStop(4);
}
if (_root.gridUP.hitTest(_x, _y-20, true)) {
this._y += 4;
_root.BG._y += 4;
_root.moveUp = true;
} else {
_root.moveUp = false;
}
if (_root.gridDOWN.hitTest(_x, _y+20, true)) {
this._y -= 4;
_root.BG._y -= 4;
_root.moveDown = true;
} else {
_root.moveDown = false;
}
if (_root.gridRIGHT.hitTest(_x+20, _y, true)) {
this._x -= 4;
_root.BG._x -= 4;
_root.moveRight = true;
} else {
_root.moveRight = false;
}
if (_root.gridLEFT.hitTest(_x-20, _y, true)) {
this._x += 4;
_root.BG._x += 4;
_root.moveLeft = true;
} else {
_root.moveLeft = false;
}
}


Inventory Codes



Insert into Frame 1:

currentslotnum = 1;
stop ();
function addToslot (item) {
if (!item.found) {
item._x = eval ("itemSlot" + currentslotnum)._x;
item._y = eval ("itemSlot" + currentslotnum)._y;
item.found = true;
currentslotnum++;
}
}


Insert into Item:

onClipEvent (enterFrame) {
if (_root.character.hitTest (this)) {
_root.addToslot (this);
}
}


Name all inventory slots:

itemSlot1
itemSlot2
itemSlot3
etc...

Name character:
character

With this code, if the "character" is touching "BG", and you press space, the "character" will jump.



onClipEvent (load) { moveSpeed = 10;}onClipEvent (enterFrame) { if (Key.isDown(Key.RIGHT)) { this._x += moveSpeed; } else if (Key.isDown(Key.UP)) { this._y -= 0; } else if (Key.isDown(Key.DOWN) && !fall) { this._y += 0; } else if (Key.isDown(Key.LEFT)) { this._x -= moveSpeed; }}onClipEvent (enterFrame) { if (Key.isDown(Key.SPACE) && !jumping) { vel_y = 36; jumping = true; } if (jumping == true) { vel_y -= 2; if (vel_y<=-15) { vel_y = -15; } this._y -= vel_y; } if (_root.BG.hitTest(this._x, this._y+35, true)) { vel_y = 0; jumping = false; }}onClipEvent (enterFrame) { this._y += 16; if (_root.BG.hitTest(this._x, this._y+1, true)) { this._y -= 16; }}

Movement with Rotation


onClipEvent (enterFrame) { if (Key.isDown(Key.UP)) { _y -= 10; _rotation = 270; }}onClipEvent (enterFrame) { if (Key.isDown(Key.DOWN)) { _y -= -10; _rotation = 90; }}onClipEvent (enterFrame) { if (Key.isDown(Key.LEFT)) { _x -= 10; _rotation = 180; }}onClipEvent (enterFrame) { if (Key.isDown(Key.RIGHT)) { _x -= -10; _rotation = 0; }}


This code is used to make walls. It also allows movement for the main symbol.

NOTE: In order for this to work, the "background" symbol must be titled "BG" in the properties box at the lower left hand corner of the screen.





onClipEvent(enterFrame){
if(Key.isDown(Key.DOWN)){
if(_root.BG.hitTest(_x, _y + 5, true)){
this._y += 4;
}
else
;
}
if(Key.isDown(Key.UP)){
if(_root.BG.hitTest(_x, _y - 5, true)){
this._y -= 4;
}
else
;
}
if(Key.isDown(Key.LEFT)){
if(_root.BG.hitTest(_x - 5, _y, true)){
this._x -= 4;
}
else
;
}
if(Key.isDown(Key.RIGHT)){
if(_root.BG.hitTest(_x + 5, _y, true)){
this._x += 4;
}
else
;
}
}




Sorry for the mess, i kinda just copyed and pasted....

thanks if you can fix these codes if they need fixing

  • 6 Replies
dank
offline
dank
986 posts
Peasant

Why are you posting this? Can't you debug a program?

Midmenj
offline
Midmenj
216 posts
Nomad

no....I'm not a good programer... and if these codes do work these would be helpful for the new programers out their

dank
offline
dank
986 posts
Peasant

All you have to do is press ctrl+shift+enter and it will debug it.

Midmenj
offline
Midmenj
216 posts
Nomad

oh really?

cool...didn't know that thanks

Captain_J_Sheridan
offline
Captain_J_Sheridan
313 posts
Nomad

I don't think debugging will help, some of these codes are just examples, you'd have to know where to put your instance names

PixelSmash
offline
PixelSmash
566 posts
Nomad

A quick comment on (at least) the enemy code... I see you've used 'onClipEvent(enterFrame)' at least four times... there's no need for that If it executes one of the _x functions, the other one doesn't need to... it can be put into one simple function...
onClipEvent(enterFrame){
if(player_x > x1){
x -=5;
} else if(player_x < x2){
x +=5;
}
if(player_y > y1){
y -=5;
} else if(player_y < y2){
y +=5;
}
}...like that. I see you've done this a couple of times, but keeping it simple and readable should be priority #1... it certainly doesn't help if you get lost in your own code!

Other than that, I can just say try it out! You learn the most from fixing the most irritating errors you yourself made!

Good luck!

Showing 1-6 of 6