building a number from mouse clicks on a sprite

rated by 0 users
This post has 13 Replies | 5 Followers

Top 100 Contributor
Posts 9
Terryb Posted: Sun, Jan 10 2010 11:28 PM

I am getting  a number (for a visual calculator) useing:

    If One.ContainsPoint ( MouseX, MouseY ) And LeftPressed Then //LeftPressed Then
                SecondPart=1
                ClearMouseEvents()
                stillrunning=False

This works in so far as geting a single (ie 0 to 9) but how do I build a larger number such as 11 or 111

Be here now

Top 10 Contributor
Posts 297
LFS replied on Mon, Jan 11 2010 9:24 AM

You are forcing the user to input keys according to your program, such as:

GetInput()
GetOperator()
GetInput2()

 What you need to do is let the user decide what to input, and have the program respond according to the key pressed. One way to do that is by using states. You'd use one input routine that would simply return the key the user clicked on and depending on the state, add that to the first number or second number. Here is a rough outline:

state = 1
While Key <> "="
   Key = UserInput()
   If [Key is a number] Then
     If State = 1 then
       FirstNumber = Firstnumber * 10 + Key
     Else
       SecondNumber = SecondNumber * 10 + Key
     End If
   Else
     Operator = Key
     State = 2
   End If
End While
ShowAnswer()

As you see the loop begins by waiting for the = sign. For each iteration it calls the UserInput() function that returns the value of the key the user hit. If the key is a number it does one thing, or if the key is an operator is does another. (That [Key is a number] won't work as shown, you'll need to add the proper test...) When the = sign is hit it breaks out of the loop to show the answer.  That is not working code, but a general idea of how to tackle the problem....  Good Luck!

Top 25 Contributor
Posts 84

Mmm, after seeing that you were making a calculator, I decided to try making one myself. This is what I had for building numbers.

Parameters:

CurNum:

The current number shown on the display of the calculator.

Button:

The one digit number of the button pressed.

This function has a built-in error: It will not go above four digits. This is to try to avoid incurring the wrath of the Int32 error gods.

Hope this works for you!

Top 500 Contributor
Posts 2
miguel replied on Wed, Feb 10 2010 6:28 AM

hello

I'm with a project for school, to make a calculator.

can you put here the one you have done?

Top 100 Contributor
Posts 9

This is verry much a work in progress and is not working as of this moment, but I have uploaded it so you are welcome to have a look and see if there is anything of use to you in there. Could I ask that if you get somthing similar upand working that I may allso see the code ?

Terry

Be here now

Top 25 Contributor
Posts 84
Megahand replied on Thu, Feb 11 2010 5:50 AM

I'll post my calculator as soon as it's finished. I've only got the operators and the display left. Well, that and I have to add the decimal point...

Unlike terryb's calculator, this calculator has the ten digits, a decimal point, the four basic operators, and an equals button. I'll add more to it over time, but it's best to start simple so that you can feel what you're working with, what techniques work, and how using different variables affects your program.

Top 25 Contributor
Posts 84

Finished with the uttermost basics. This calculator supports positive integer numbers up to 9999. It cannot do decimals or negatives yet.
It does refresh the display, rather sloppily* though. Improving that is on the list.
It doesn't have the ability to do things like 1+1+1= yet, you have to press 1+1=+1=.
The ability to press = to repeat the last operation is not yet available. This means that 1+1== does not give three, it gives error.
Other than that, enjoy!

 

*sloppily in this case just means that the timing can make no sense.

Top 100 Contributor
Posts 9
Terryb replied on Fri, Feb 12 2010 12:51 AM

Bieing new to phrogram I'm at a loss to understand the following code from Megahand's calcutor ;code fragment -

"Buttons.Item((I - 1) * 4 + J).MoveTo(J * 5 + ((J - 1) * 25), 20 + I * 5 + ((I - 1) * 25))
                Buttons.Item((I - 1) * 4 + J).SetRotationOffset(-12.5, -12.5)
                Buttons.Item((I - 1) * 4 + J).Visible = True"

Can some one please explain,

I can see that this code is placing and showing the buttons but thats about all !

PS Thank you Mehahand for sharing you code

Be here now

Top 10 Contributor
Posts 559
davidw replied on Fri, Feb 12 2010 8:48 AM

This is very cool!

Megahand will know best how this code works but within a 4 by 4 nested loop, it does look like it is  populating and placing the buttons from the spritelist. When you hover over code in Phrogram, you will see a pop-up tip on what the method does. On the second line of this excerpt, It looks like Megahand is using SetRotationOffset to compensate for the bars in the column and row bars that are visible in the backdrop image.

Look forward to seeing more functionality in this program - it would have been a great entry into our math contest a year or two back!

Top 10 Contributor
Posts 297
Seems like a neat little weekend project.... But, what if the user enters: 2 + 3 * 5 If the program does the operations as they are entered, it will add 2+3 to get 5 and multiply that by 5 to get 25. But if the program has to follow normal mathematical conventions, then the program should do the multiplication first, followed by the add operation to get 17. It would appear the program has to save all of the input until the = key is pressed and then apply mathematical conventions (precedence) to the entire equation. Try Window's own claculator to see what it does.... I've have attached an example of entering numbers on a key pad. Until the = key is pressed, it is just so much text that has to be appended. The operand is appended for each number hit, and the equation is appended when an operation key is hit. Have look and see if it helps you get farther along....
Top 25 Contributor
Posts 84
Megahand replied on Sat, Feb 13 2010 11:00 AM

True, but I'm working on that...
...and negatives...
...and decimals...
...and more functions...
...and making the calculator look a little nicer...
...and memory. And fractions.

Oh, and, yes, that is what the nested Fors do, and the rotational offset was because I was originally planning to make the buttons turn 180 degrees when you pressed them. The problem was that the numbers themselves also rotated. You can delete the offset setting line.

PS You're welcome, TerryB :D

Top 500 Contributor
Posts 2
miguel replied on Sun, Feb 14 2010 7:17 AM

I'm so new in this, my calculator is going to be much more simple

I just want to ask the user for a number then the operation he wants to do and then the other number. and if the presses F to end the program.

My problem is to finish the program when F is pressed, I've been unable to do that.

Page 1 of 2 (14 items) 1 2 Next > | RSS