00:00
00:00
Newgrounds Background Image Theme

LewgusWithFriends just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

a little help with my AS3

351 Views | 4 Replies
New Topic Respond to this Topic

a little help with my AS3 2017-07-20 05:33:22


hey i was curious if anyone could help me get my AS3 script working properly there is a few bugs i cant seem to sort out.

//speed variable//
var yvel = 0;
var xvel = 0;

var GRAV = 1;
var FRIC = .95;

//health

var maxHP:int = 100;
var currentHP:int = maxHP;
var percentHP:Number = currentHP / maxHP;

//Timer

//var nCount:Number = 300;
//var myTimer:Timer = new Timer(1000, nCount);
//timer_txt.text = nCount.toString();
//myTimer.start();
//myTimer.addEventListener(TimerEvent.TIMER, countdown);

//function countdown(e:TimerEvent):void
//{
// nCount--;
// timer_txt.text = nCount.toString();
//
// if(nCount == 0){
// myTimer.stop();
// myTimer.removeEventListener(TimerEvent.TIMER, countdown);
// gotoAndStop(2);
// }

//}

//sounds

var bubblesound = new bubble();

//end sounds

var rdown = false;
var ldown = false;
var jdown = false;

var dude = bg.ball;
var d =dude;

var onground = false;

var downarr = new Array(-10, 0, 10);
var vertarr = new Array(-25, -35);
var uparr = new Array(-3, 0, 3);

var numjumps = 0;

//defines whether the player is touching the left or right wall

var onwalll = false;
var onwallr = false;

//defines whether all conditions to wall slide are true

var wallslidingl = false;
var wallslidingr = false;

bg.ground.visible = false;
bg.cloudbg.visible = false;

//enemies array

var enemies = new Array();

this.addEventListener(Event.ENTER_FRAME, eFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, kDown);
stage.addEventListener(KeyboardEvent.KEY_UP, kUp);
//test hit button - blue circle onscreen
circle.addEventListener(MouseEvent.CLICK,min)

//enemy function

function addEnemy(ref)
{
enemies.push(ref);
ref.xvel = 0;
ref.yvel = 0;
//random speed when bug spawns
//ref.sp = Math.random()*3+2;
//give random direction
ref.dir = Math.floor(Math.random()*2);
if (ref.dir==0)
{
ref.dir =-1;
ref.scaleX = -1;
}
}

//healthbar update

function updateHealthBar():void
{
percentHP = currentHP / maxHP;
healthbar.hpbarcolor.scaleY = percentHP;
}

function min(e:MouseEvent){
healthbar.hpbarcolor.scaleY-=0.05
if(healthbar.hpbarcolor.scaleY<=0){
gotoAndStop(2)
}
}
//keyboard events

function kDown(e: KeyboardEvent) {
var code = e.keyCode;
if (code == 37) {
ldown = true;
}
if (code == 39) {
rdown = true;
}
if (code == 38) {
if (!jdown && numjumps < 2) {
//adjust the logic for triggering a jump to account for wall slider lefot or right
if (!wallslidingl && !wallslidingr) {
runJump();
} else {
//re-enable double jumping when jumping off a wall
numjumps = 1;
onground = true;
//if wall sliding left jump off to the right and vice versa
if (wallslidingl) {
xvel = 15;
} else {
xvel = -15;
}
//jump upwards when wall jumping
yvel = -16;

//trigger spin animation when walljumping
dude.guts.gotAndStop("spin");
}
}
jdown = true;
}
}

function runJump(daheight = -16) {
if (numjumps >= 1) {
onground = false;
dude.guts.gotoAndStop("spin");
bubblesound.play();
} else {
//numjumps++; is adding one jump to the number of jumps
dude.guts.gotoAndStop("jump");
bubblesound.play();
}
numjumps++;

yvel = daheight;
}

function kUp(e: KeyboardEvent) {
var code = e.keyCode;
if (code == 37) {
ldown = false;
//when the left button is released turn off wall sliding variables associated with the left
onwalll = false;
wallslidingl = false;
}
if (code == 39) {
rdown = false;
//when the right button is released turn off wall sliding variables associated with the right
onwallr = false;
wallslidingr = false;
}
if (code == 38) {
jdown = false;
}
}

function makePoof(x, y, sc) {
var p = bg.addChild(new poof());
p.x = x;
p.y = y;
var ran = Math.ceil(Math.random() * 5);
p.gotoAndPlay(ran);
p.scaleX = sc;
}

function controls() {
if (rdown) {
rbox.text = "true";
xvel += 1;
dude.scaleX = 1;
if (onground && numjumps == 0) {
if (xvel > 0) {
dude.guts.gotoAndStop("move");
} else {
dude.guts.gotoAndStop("skid");
if (onground && numjumps == 0) {
makePoof(dude.x, dude.y, dude.scaleX);
}
}
}
} else {
rbox.text = "false";
}

if (ldown) {
lbox.text = "true";
xvel -= 1;
//dude is bg.ball
dude.scaleX = -1;
if (onground && numjumps == 0) {
if (xvel < 0) {
dude.guts.gotoAndStop("move");
} else {
dude.guts.gotoAndStop("skid");
if (onground && numjumps == 0) {
makePoof(dude.x, dude.y, dude.scaleX);
}
}
}
} else {
lbox.text = "false";
}

if (jdown) {
jbox.text = "true";
} else {
jbox.text = "false";
}

//adjust animation logic to account for NOT wall sliding left and right
if (!onground && !wallslidingr && !wallslidingl && numjumps <= 1) {
dude.guts.gotoAndStop("jump");
}

//adjust animation logic to account for a double jump and NOT wall sliding left and right
if (!onground && !wallslidingr && !wallslidingl && numjumps > 1) {
dude.guts.gotoAndStop("spin");
}

//adjust animation logic to play wall sliding animation while wall sliding left or right
if (wallslidingr || wallslidingl) {
dude.guts.gotoAndStop("slide");
}

//adjust returing to idle on if you ar NOT wall sliding
if (!ldown && !rdown && onground && numjumps == 0 && !wallslidingr && !wallslidingl) {
dude.guts.gotoAndStop("idle");
}

}

function centerWindow() {
var newx = -dude.x +375;;
bg.x = newx;
//bg.x = newx;
var distx = newx - bg.x;
bg.x += distx / 10;

var newy = -dude.y + 310;;
bg.y = newy;
//bg.y = newy;
var disty = newy - bg.y;
bg.y += disty / 10;

//right edge level multiplied by -1
//if (bg.x < -750) {
//bg.x = -750;
//}
//left edge level stop scrolling
//if (bg.x > 0) {
//bg.x = 0;
//}

//parallax
midbg.x = bg.x * .15;
mountains.x = bg.x * .05;
cloudbg.x = bg.x * .03;
fg.x = bg.x * 1.5;

midbg.y = bg.y;
mountains.y = bg.y * .35;
cloudbg.y = bg.y * .15;
fg.y = bg.y;
}

function phys() {
bg.ball.x += xvel;
bg.ball.y += yvel;

xvel *= FRIC;

//decrease gravity while sliding down a wall
if (wallslidingr || wallslidingl) {
yvel += GRAV * .5;
if (yvel > 5) {
yvel = 5;
}
} else {
//if your not sliding either way return to normal gravity
yvel += GRAV;
if (yvel > 14) {
yvel = 14;
}
}

if (yvel > 5) {
onground = false;
}

//adjust the order that the player is pushed by theses functions to prevent
//teleporting through walls

downPush(dude);
leftPush(dude);
rightPush(dude);

upPush(dude);
upPushCloud(dude);

}

//**dude.virtx = dude.x+bg.x;
//**dude.virty = dude.y+bg.y;

//old hit test method//
//while(bg.ground.hitTestPoint(dude.virtx,dude.virty,true))
//{
// onground = true;
// dude.virty--;
// dude.y--;
// yvel = 0;
//}

function downPush(d) {
d.virtx = d.x + bg.x;
d.virty = d.y + bg.y;

for (var i = 0; i < uparr.length; i++) {
var num = uparr[i];
var tx = d.virtx + num;
var ty = d.virty - 40;
while (bg.ground.hitTestPoint(tx, ty, true)) {
d.virty++;
ty++;
d.y++;
yvel = 1;
}
}
}

function rightPush(d) {
//remove the "+xvel" from the calculaion so the hittesting doesn't "look ahead"

d.virtx = d.x + bg.x;
d.virty = d.y + bg.y;
onwallr = false;

for (var i = 0; i < vertarr.length; i++) {
var num = vertarr[i];
var tx = d.virtx + 21;
var ty = d.virty + num;

while (bg.ground.hitTestPoint(tx, ty, true)) {
//only register a wall touch if player's yvel is greater than 0 (heading
//downward) and then you have to be actively pressing into a wall
//to initiate a wall touch/slide
if (yvel > 0 && rdown) {
onwallr = true;
}
//onground = true;
d.virtx--;
d.x--;
tx--;
xvel = 0;
}

}
}
//if your not actively pressing right button shortcircuit the wall slide
if (!rdown) {
onwallr = false;
wallslidingr = false; {

//make sure you're in the air for a wall slide
if (!onground || numjumps !== 0) {
if (onwallr && rdown) {
wallslidingr = true;

Response to a little help with my AS3 2017-07-20 05:34:52


i can upload my fla if that would make it easier.
basically everything seems to work except my enemies dont move and i cant get my wall slide/wall jump to work.

Response to a little help with my AS3 2017-07-20 11:29:43


At 7/20/17 05:34 AM, ArtByXell wrote: i can upload my fla if that would make it easier.
basically everything seems to work except my enemies dont move and i cant get my wall slide/wall jump to work.

Can you go into more detail as to what the problems are? Are the enemies literally not moving at all, ever? What isn't working with the wall jumping/sliding (i.e. what happens that shouldn't be happening and/or what doesn't happen that should)?


At 7/20/17 11:29 AM, Diki wrote:
At 7/20/17 05:34 AM, ArtByXell wrote: i can upload my fla if that would make it easier.
basically everything seems to work except my enemies dont move and i cant get my wall slide/wall jump to work.
Can you go into more detail as to what the problems are? Are the enemies literally not moving at all, ever? What isn't working with the wall jumping/sliding (i.e. what happens that shouldn't be happening and/or what doesn't happen that should)?

the enemies move but the dont move left and right.

as for the wall jump. it should allow me to slide down any walls. and also when i do start to wall slide it should allow me to double jump again.

ive been stuck for a few days now.

just trying to add new features in the mean time.

working on a retro filter effect. rgb displacment havent quite got that working either..

im from ontario also by the way. nice to see more canadians on NG.

like i said i could send you my fla if it would help

Edit: when i attempt a wall jump it just doesnt happen

Response to a little help with my AS3 2017-07-20 14:11:02