How do I do this? (actionscript 2.0 character turning) - actionscript-2

I am trying to make my character go down/up left/right
using this code (example)
if (Key.isDown(Key.LEFT, Key.DOWN))
{
_x -= power;
_root.char.gotoAndStop(6);
}
}
But when doing so this also registering the left key to frame 6 so it goes down left when I press left key..

You need a separate condition for each key, if you want each key to do something unique. Note that you'll need to modify the frame number specified in _root.char.gotoAndStop(6) for each respective key press.
if (Key.isDown(Key.LEFT))
{
_x -= power;
_root.char.gotoAndStop(6);
} else if (Key.isDown(Key.DOWN))
{
_y += power;
_root.char.gotoAndStop(6);
} else if (Key.isDown(Key.RIGHT))
{
_x += power;
_root.char.gotoAndStop(6);
} else if (Key.isDown(Key.UP))
{
_y -= power;
_root.char.gotoAndStop(6);
}

Related

How do I add to my int variables when I press a key?

The title summed up what I am trying to figure out.
I am trying to get my int variables to increase and/or decrease depending on what directional key I press.
int x;
int y;
x = 0;
y = 0;
Console.WriteLine("X:" +x + "Y:" +y);
while (Console.Readkey().Key == ConsoleKey.UpArrow)
{
y = +1;
}
If I press the Up arrow nothing happens, if I press any other directional keys it will exit the Console. I fell that I am on the right track because of that alone.
Any help is awesome.
Here you go. I've written this in a way that I'm assuming does what you want it to do.
int x = 0;
int y = 0;
while (true)
{
Console.WriteLine("X:" + x + "Y:" + y);
ConsoleKey key = Console.ReadKey().Key;
Console.Clear();
if (key == ConsoleKey.UpArrow)
{
y += 1;
}
else if (key == ConsoleKey.DownArrow)
{
y -= 1;
}
else if (key == ConsoleKey.RightArrow)
{
x += 1;
}
else if (key == ConsoleKey.LeftArrow)
{
x -= 1;
}
}
It saves the current key in ConsoleKey key and then checks which direction was pressed, and modifies x or y as necessary.
Note that I also added a Console.Clear() so everything prints neatly instead of repeatedly one after another.
Brackets {} should appear after a while statement, and the k in "Readkey" should be capitalized (e.g. "ReadKey")
while (Console.ReadKey().Key == ConsoleKey.UpArrow)
{
y = +1;
}
Edit: There was as semi-colon after the while statement, making the loop null. This version should work.

Tile based movement on Actionscript 2.0 (adobe flash cs6)

So I have got this problem where I do not know how to possible code tile based movement in 4 directions (NSWE) for action script 2.0.
I have this code but it is dynamic movement, which makes the char move in all 8 directions (NW,NE,SW,SE N,S,W,E). The goal is to limit the movements to tile based and in only 4 directions (NSEW)
onClipEvent(enterFrame)
{
speed=5;
if(Key.isDown(Key.RIGHT))
{
this.gotoAndStop(4);
this._x+=speed;
}
if(Key.isDown(Key.LEFT))
{
this.gotoAndStop(3);
this._x-=speed;
}
if(Key.isDown(Key.UP))
{
this.gotoAndStop(1);
this._y-=speed;
}
if(Key.isDown(Key.DOWN))
{
this.gotoAndStop(2);
this._y+=speed;
}
}
The most simple and straightforward way is to move that thing along X-axis OR along Y-axis, only one at a time, not both.
onClipEvent(enterFrame)
{
speed = 5;
dx = 0;
dy = 0;
// Figure out the complete picture of keyboard input.
if (Key.isDown(Key.RIGHT))
{
dx += speed;
}
if (Key.isDown(Key.LEFT))
{
dx -= speed;
}
if (Key.isDown(Key.UP))
{
dy -= speed;
}
if (Key.isDown(Key.DOWN))
{
dy += speed;
}
if (dx != 0)
{
// Move along X-axis if LEFT or RIGHT pressed.
// Ignore if it is none or both of them.
this._x += dx;
if (dx > 0)
{
this.gotoAndStop(4);
}
else
{
this.gotoAndStop(3);
}
}
else if (dy != 0)
{
// Ignore if X-axis motion is already applied.
// Move along Y-axis if UP or DOWN pressed.
// Ignore if it is none or both of them.
this._y += dy;
if (dy > 0)
{
this.gotoAndStop(2);
}
else
{
this.gotoAndStop(1);
}
}
}

'Grenade' projection based on angle + bounce

I'm having some trouble with synthesizing an advanced object projection formula. I have already figured out few basic physic simulation formulas such as:
Velocity of object:
x += cos(angle);
y += sin(angle);
*where angle can be obtained by either mouse position or with tan(...target and intial values)
but that only travels straight based on the angle.
Gravity:
Yvelocity = Yvelocity - gravity;
if(!isHitPlatform) {
Obj.y += YVelocity
}
Bounce:// No point if we've not been sized...
if (height > 0) {
// Are we bouncing...
if (bounce) {
// Add the vDelta to the yPos
// vDelta may be postive or negative, allowing
// for both up and down movement...
yPos += vDelta;
// Add the gravity to the vDelta, this will slow down
// the upward movement and speed up the downward movement...
// You may wish to place a max speed to this
vDelta += gDelta;
// If the sprite is not on the ground...
if (yPos + SPRITE_HEIGHT >= height) {
// Seat the sprite on the ground
yPos = height - SPRITE_HEIGHT;
// If the re-bound delta is 0 or more then we've stopped
// bouncing...
if (rbDelta >= 0) {
// Stop bouncing...
bounce = false;
} else {
// Add the re-bound degregation delta to the re-bound delta
rbDelta += rbDegDelta;
// Set the vDelta...
vDelta = rbDelta;
}
}
}
}
I need help way to combine these three formulas to create an efficient and lightweight algorithm that allows an object to be projected in an arch determined by the angle, yet continues to bounce a few times before coming to a stop, all with an acceptable amount of discontinuity between each point. *Note: Having the grenade be determined by a f(x) = -x^2 formula creates a larger jump discontinuity as the slope increases, forcing you to reverse the formula to find x = +-y value (to determine whether + or -, check the bounds).
something like:
class granade
{
private static final double dt = 0.1; // or similar
private double speedx;
private double speedy;
private double positionx;
private double positiony;
public granade(double v, double angle)
{
speedx = v * Math.cos(angle);
speedy = v * Math.sin(angle);
positionx = 0;
positiony = 0;
}
public void nextframe()
{
// update speed: v += a*dt
speedy -= gravity* dt;
// update position: pos += v*dt
positionx += speedx * dt;
double newpositiony = positiony + speedy*dt;
// bounce if hit ground
if (newpositiony > 0)
positiony = newpositiony;
else {
// bounce vertically
speedy *= -1;
positiony = -newpositiony;
}
}
public void draw() { /* TODO */ }
}
OT: avoid Math.atan(y/x), use Math.atan2(y, x)

I am getting an error (variable y might not have been initialised in the if's in the loop. How to solve that?

import java.util.Scanner;
public class RPS
{
public void show()
{
int i;
System.out.println("1 - Rock 2 - Paper 3 - Scissor");
Scanner in = new Scanner(System.in);
i = in.nextInt();
double x = Math.random();
int y;
if(x<=0.33)
{
y=1;
}
else if(x>0.33 && x<0.67)
{
y=2;
}
else if(x>=0.67)
{
y=3;
}
for(;;)
{
if(i==y)
System.out.println("It's a draw!");
else if(i==1 && y==2)
System.out.println("Computer wins!");
else if(i==1 && y==3)
System.out.println("You win!");
else if(i==2 && y==1)
System.out.println("You win!");
else if(i==2 && y==3)
System.out.println("Computer wins!");
else if(i==3 && y==1)
System.out.println("Computer wins!");
else if(i==3 && y==2)
System.out.println("You win!");
else
System.out.println("Error!");
}
}
}
Whats wrong?
It gives an error that variable y might not have been intialised in the if's in the for loop.
I have assigned a value to y in the previous if-else section.
so why isnt it getting intialised?
javac is not smart enough to realize that the way your conditions are constructed, one of them will always be true.
You can rewrite your if-statements to make javac realize one branch will always be triggered:
int y;
if(x<=0.33)
{
y=1;
}
else if(x>0.33 && x<0.67)
{
y=2;
}
else // This only triggers when x >= 0.67, so no need to check
{
y=3;
}
Now javac sees that if the first two don't trigger, the last will, so y will always have a value.
You can alternatively add an else branch with an error, in case someone breaks the conditions:
int y;
if(x<=0.33)
{
y=1;
}
else if(x>0.33 && x<0.67)
{
y=2;
}
else if(x >= 0.67)
{
y=3;
}
else
{
// This should never happen
throw new IllegalArgumentException("Something's gone terribly wrong for " + x);
}
This also compiles, and if someone later decides to skew the numbers and turns the first condition into x <= 0.2 but forgets to update the other condition, you'll get an exception at runtime.

WP8 implementing gestures without previous WP7 toolkit

I've recently rebuilt an app for Windows Phone 8, but with the new Silverlight Toolkit, GestureListener is no longer present, warning: "GestureListener is not supported in a Silverlight project". I really want to implement a gesture navigation system into my app whereby the page can be swiped left or right to navigate to one of two other pages, but only after a certain "drag threshold" - this was nicely shown in WP7 here (the behaviour for deleting items I would like to apply to my MainPage) - but without the old controls I can't see a clear way to do this, after trying relentlessly. Now there are apparently only three Manipulation Events we can use, which has complicated a process that was so much easier before. I'm trying to make the whole page (ie. the first ContentPanel) moveable along the horizontal, but can't even achieve this now. Please could somebody help, in any way?
using Microsoft.Phone.Controls;
namespace PhoneApp2
{
public partial class MainPage : PhoneApplicationPage
{
double _x = 0;
double _y = 0;
double _x2 = 0;
double _y2 = 0;
public MainPage()
{
InitializeComponent();
}
private void PhoneApplicationPage_ManipulationStarted_1(object sender, System.Windows.Input.ManipulationStartedEventArgs e)
{
_x = e.ManipulationOrigin.X;
_y = e.ManipulationOrigin.Y;
}
private void PhoneApplicationPage_ManipulationCompleted_1(object sender, System.Windows.Input.ManipulationCompletedEventArgs e)
{
_x2 = e.ManipulationOrigin.X;
_y2 = e.ManipulationOrigin.Y;
string _xx = string.Format(" x:{0} y:{1} x2:{2} y2:{3}", _x, _y, _x2, _y2);
if (_y > _y2 && _y - _y2 > 100)
{
lbl1.Text = "up" + _xx;
}
else if (_x > _x2 && _x - _x2 > 100)
{
lbl1.Text = "left" + _xx;
}
else if (_y < _y2 && _y2 - _y > 100)
{
lbl1.Text = "down" + _xx;
}
else if (_x < _x2 && _x2 - _x > 100)
{
lbl1.Text = "right" + _xx;
}
}
}
}