i put this under actionscript instead of games because it fits this category better.
I came up with some code to ad friction to the puck. The prblem is I can only get it to move on the y axis. I am not sure on how to go about moving it on the x and y.
I also have the paddle constrained to a rectangle with a startdrag function.
I just need help with moving the ball on the x and y axis and possibly inersha (for example, if the paddle hits the puck hard than the puck will go faster).
I have the hitTests working great. I have them checking inbetween frames so the puck wont pass right through the paddle. anyways. please help
In my version, I use the variables vx and vy to represent velocity - this is the same as your 'ymov'. If you zero out the gravK (gravity) constant, you'll have something you can use for Hockey or Billiards.
Your script is functionally equivalent to this, which more accurately represents what is going on - you were mixing up ricochets with damping by using a negative damping factor.
Code:
ymov = 19;
decay = .98;
_root.onEnterFrame = function()
{
// reverse y velocity if we hit something
if (puck.hitTest(paddle) ||
puck.hitTest(_root.tw))
{
ymov = -ymov;
}
// slow down over time
ymov *= decay;
// add velocity to position
puck._y += ymov;
};
As you noted, you are currently not doing any thing with the x coordinate.
You are also not currently tracking the velocity of the paddle, which you will need to do if the paddle is going to confer energy to the puck.
In my superball script, which I mentioned above, when you move one of the balls with the mouse, it acts as a paddle would need to act in your script. The mouse motion is tracked and used to set the velocity of the ball that is being dragged.
Thank you for the help so far. I understand where you are going with this. Here is my fla. please tell me where i went wrong? I appreciate it. This is one of those things where I have to see it in order to learn it. You know? You dont have to teach me how to do it just show me how.
I no why this doesnt work. Its missing alot of script lol. For instance moving the puck on both planes. This is my fault though. I didnt tell it too. Anyways. anymore help would be awsome.
i took you superballs script and made it only have two balls but how do i make it so that I can have 2 different sized balls on the screen? hence on being for the puck and the other for the paddle?
Thank you very much Jim. Your script was very helpful and I understand it surprisingly. . If I need anymore help would it be alright to IM you or something. If not its cool.
i made a hit test for the right goal. So when the puck comes and hits it, it will add one to your score and place the puck on yourside of the center line. It works great but only when the puck hits it at slow speeds. Is there anyway to have the hit test check every half a frame instead of everyframe? or get the
puck.radius = 9;
puck.width = puck.radius*2;
i dont even know if something like this is possible but yea.
I guess it could be called detection using math. It seems to me to be more accurate.
since its hitting a line maybee try to add the equation of a line
y = mx+b;
so its accurate
also when the puck is moving at high speeds it doesnt look like its hitting the walls. is that just because its moving fast?
The hit test works great only when you hit the center point of the mc. The problem with this code is that when you use._x or ._y, Flash will only trace where you declared the center of the mc and not the whole thing. So maybee we could try making a mc (rgl) by creating it in actionscript. for example rectangle = [ t , l , r, b ] and then using it in a mathimatical hit test. saying puck._x >= rectangle. i dont know. lol. Am i at least correct about the ._x and ._y? im still new to action script but i know my trig. (I am in a trig class right now in school)
maybee even doing something like this
rgl = {};
rgl._width = 10;
rgl._height = 90;
rgl.area = rgl._width*rgl._height;
rgl._x = 500;
rgl._y = 200;
is this even pssible to do?
and then creating the hitest so it knows all the parameters of the mc rgi. would that work or am I way off?
anyways
here is the new fla.
or
code:
decay = .98;
score1 = 0;
score2 = 0;
_root.onEnterFrame = function() {
puck.xmov *= decay;
puck.ymov *= decay;
puck._x += puck.xmov;
puck._y += puck.ymov;
// reverse x velocity if we hit left or right
if (puck._y+puck.ymov<79)
{
puck._y = 79;
puck.ymov = -puck.ymov;
}
if (puck._y+puck.ymov>73+243)
{
puck._y = 73+243;
puck.ymov = -puck.ymov;
}
if (puck._x+puck.xmov<70)
{
puck._x = 70;
puck.xmov = -puck.xmov;
}
if (puck._x+puck.xmov>69+462)
{
puck._x = 69+462;
puck.xmov = -puck.xmov;
}
// basic movement
rgl._alpha = 0;
// if we hit the paddle, move the puck
if (puck.hitTest(paddle)) {
puck.xmov = paddle.vx;
puck.ymov = paddle.vy;
}
// if puck hits the goal line, add 1 to score and move it to the appropiate place
if (puck._x >= rgl._x &&
Math.abs(puck._y - rgl._y) < rgl._width/2)
{
score1 += 1;
puck._x = 250;
puck._y = 200;
puck.ymov = 0;
puck.xmov = 0;
if (score1 == 7){
//Ill add a you win and stuff like that in the second frame
}
}