ForumsProgramming ForumHelp with some newbie basic code

2 3039
Dannydaninja
offline
Dannydaninja
948 posts
Nomad

Hi, i need help with some programming for this game i'm making. All i need is some answers to these questions most of you experinced programmers should find quite simple.

How can i stop my guy going on of the screens boundries using this code?

onClipEvent (load) {
power = 3;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
_x -= power;
}
if (Key.isDown(Key.RIGHT)) {
_x += power;
}
if (Key.isDown(Key.UP)) {
_y -= power;
}
if (Key.isDown(Key.DOWN)) {
_y += power;
}
}


Let's say my sprite has a face right? how can i make it that if i press the left key ( which would make him go left ) how could i make him look towards to way he's walking?


And, how would i do the same thing but with a gun? Also how would i make him shoot? like how could i have an object fire from out of the gun??

Okay, now how could i make it that an object could move in a random direction???

Finally, how could i make an object bounce/ rebound of another object???? ( like a wall )


Aaanddd ( THE LAST QUESTION , let's say i've got a green circle and the green circle collides with a " Red " Object. How could i make the red circle to turn into a green circle when it has touched a green circle? ( Kind of like zombies ) Then the new green circle can turn other red objects into more zombies?!?!??

  • 2 Replies
AbnormalIdiot
offline
AbnormalIdiot
110 posts
Nomad

To stop from leaving the screen, add something like (this one is for moving left, but you can adapt it for the other directions) && _x > 0

for the changing which direction it faces, make a variable, let's say, scale. Set it's value to the object's xscale. Then do something like, if the left arrow key is pressed, _xscale = -scale; and if the right key is pressed, _xscale = scale;

gun, same thing but use _root.character.gun, and make a different variable for it's xscale.
For shooting, do something like (this is assuming the max distance you want the bullet to be from the character's position).

var bullet = _root.attachMovie("Bullet", "Bullet" + _root.getNextHighestDepth(), _root.getNextHighestDepth());
bullet._x = _x + Math.cos(_root.character.gun._rotation*(Math.PI/180))*25;
bullet._y = _y + Math.sin(_root.character.gun._rotation*(Math.PI/180))*25;


Random direction: give the object variables for xspeed and yspeed.
in the onLoad function: xspeed = Math.random()*10; and do the same for yspeed.

Bouncing, simply do something like:
if(this.hitTest(_root.wall) && xspeed > 0) // the xspeed > 0 part is to make sure you don't get stuck in the wall.
{
xspeed *= -1;
}


There is a way to do it with changing the color directly, but I'm not sure how to do it. Here's a workaround though:
Make a frame for it when it's green, and one for when it's red. in it's onLoad (assuming red is the first frame, and you want it to start that way) gotoAndStop(1); //or in this case, just stop();

Then do something like (assuming you created other balls under the var balls:
for(var i in balls)
{
if(this.hitTest(balls[i])
{
gotoAndStop(2); // to make it the other color.
}



Hope that helped, good luck with your game!

Dannydaninja
offline
Dannydaninja
948 posts
Nomad

I don't actaully need for it to change color, i need it to turn into the ' zombie ' object that touched him. so it would change into the object and then become a fellow zombie.

Showing 1-2 of 2