This question already has an answer here:
XNA Handling a Combination of key presses
(1 answer)
Closed 8 years ago.
I am working on a tutorial part for my game for moves.
Each move requires 1 or more keyboard keys to be pressed.
Once the move is pressed, the next move to be pressed is displayed on the screen.
How do I go about creating predefined lists of these and cycle through them based on whether last one is correctly input? (I've got a few pretty long sequences... should I code them in XML, etc? It's a game based on combo sequences. How about creating some type of system for it?
Any advice or advice on a good tutorial would be much appreciated!
Thanks!
Couldn't you create an array of Keys objects:
Keys[] TutorialMove = new Keys[6]; // <- 7 Key Stroke Combo for move
And then populate it or:
Keys[] TutorialMove = {
Keys.Left,
Keys.Up,
Keys.Space
}
// A three combo move in that order.
And then you could call this with:
short Strokes = 0;
short MoveLen = TutorialMove.Length();
public override void Update(GameTime gameTime)
{
// Hopefully you already created this variable in Initialize()
currentKeyboardState = Keyboard.GetState();
if (currentKeyboardState.IsKeyDown(TutorialMove[Strokes]))
{
Strokes += 1;
if (Strokes == MoveLen)
{
// End Tutorial (or change move sequence)
}
}
}
And that should do what you want. That way, you can easily define your combo's as a list in the format of the second suggestion. Or write the Key Presses in a text file, and make a method to read them and assign the to you array.
Hope this helped, Mona.
Related
I have a map where a user can choose some object/feature on the map and draw a line to another object/feature. When the user selects the feature i would like do add a draw interaction and already set the first point to the selected feature without the user having to click again on the map.
Here is a fiddle: Sample
The commented code below should be executed programmatically without user interaction, after pressing the draw button
geometryFunction: function (c, g) {
if (goog.isDef(g)) {
g.setCoordinates(c);
} else {
// DO THIS AUTOMATICALLY ON PRESSING DRAW
// TO INITIALIZE AND START THE DRAWING PROCESS
c[0][0] = 1174072.754460305;
c[0][1] = 332653.94709708635;
g = new ol.geom.LineString(c);
}
...
}
The current behaviour is that you click on the Draw button and can click on the map to start drawing (but i overwrite the first node with my desired starting location -- in this example near central africa)
Is it possible to click on Draw and the first node is already programmatically set, without having to click on map first?
It is not currently possible to do manually append points to the OpenLayers 3 ol.interaction.Draw, but it would make sense to be able to support it (in my mind). It would be "as-if" the user had clicked.
You should ask the OL3-dev mailing this about adding such a feature to see what they think about it. If they agree and you're willing to work on this, you could provide a pull request. See: https://groups.google.com/forum/#!forum/ol3-dev
If you don't mind using a private method in OL you can do this to achieve what you want.
var event = $.Event('click'); //create a click event in your draw method using JQuery
event.coordinate = [1174072.754460305,332653.94709708635];// set your starting coordinate
draw_interaction.startDrawing_(event);// tell your interaction to start drawing
I am learning Xcode(objective C). I want to make simple calculator.
I started by adding four simple buttons and one label. Two buttons are for numbers(1 and 2), and I added variables into .m file:
int *one = 1;
int *two = 2;
Next step what I've done is that I made action on button clicked and said that label take value from that button:
self.firstLabel.text=[NSString stringWithFormat:#"%d", one);
And I made same action for another button.
Everything is fine until this step, what I want to do next is when I click on number button to add value to that label, then when I click to divide(/) button to add / sign and when I click to another button to add another value.
I want to be something like this in label: 2 / 1, and when i click = button to divide them and show result. Thanks.
EDIT:
I also added label proper into .h, and when I click on button it shows me only one value in label.
Since you are just starting to learn obj C you may want to keep your calculator simple, building a fully functional calc. can get surprisingly complex. You should consider using an RPN approach, it will make things much easier, you can just push the numbers to a stack and perform operations one at a time.
Something like: 2, enter, 1, enter + enter.
You may also want to have a look at Stanford's iOS course on Apple University, the course is in Swift but the class project was a calculator so it should give you a good reference point. Hope that helps and good luck!
you should add a custom target for each button, i.e.,
- (IBAction)addDivide:(id)sender {
self.firstLabel.text = [[NSString alloc] initWithFormat:#"%# /", self.firstLabel.text];
}
Any action will update your label.
Just a further advice for you, don't name the label as "firstLabel", try to give it a name tied to its semantics.
You are only setting the int value to the label. You need to append the value maintaining the previous text so that it appears as "2 / 1".
I'll suggest using tags for the buttons and use a common method for number buttons in your calculator.
This is more sort of logical thing rather than specific to iOS and Xcode.
Similarly when "=" is pressed use a method to calculate the result.
Before I start I want to explain that I'm looking for a general solution to my inquiry, I don't have any code to show because I haven't the faintest idea of how to start. Let's say I have a program in which I have 4 buttons, two on the right (red and blue), two on the left (white and yellow). If I were to press a certain combination of buttons I want to get a specific output an example would be if I touch the red button and then the white one the word "cow" appears or if were to touch the white one then the red one the word "chicken" appears. Is there any documentation that anyone knows of that can help me with my problem? Can this even be done with an array?
HotLicks has a valid answer, but I think an NSDictionary is a better solution. Create keys based on button presses, like:
#{ #"RedWhite" : #"Cow",
#"WhiteRed" : #"Chicken" };
Your code will be easier to read, and you can support three presses easily (assuming you're working with a timer that you invalidate after each press) by just adding another key; no recalculating your array:
#{ #"RedWhiteBlue" : #"America" };
Hi I am developing a application where I need to display number of images with some conditions. The images should display one by one on the screen when next button is clicked.Here the size of all images is 360x480.When the image is display then all other fields like next and previous buttons should be added to the screen.How to paint those images.Can anybody please help me in this regard.
You can display images with the BitmapField. Use the setBitmap() method to change the image.
Edit: Ok, so you would want to do something like this, put the names of all your bitmaps in an array, have the button's listeners increment or decrement the array index and display that bitmap. So your next button might look like this:
ButtonField nextButton = new ButtonField("Next");
nextButton.setChangeListener(new FieldChangeListener(){
public void fieldChanged(Field field, int context)
{
if(arrayIndex + 1 >= bitmapArray.length)
{
return;
}
arrayIndex++;
Bitmap image = Bitmap.getBitmapResource(bitmapArray[arrayIndex])
myBitmapField.setBitmap(image);
}
});
This assumes that your images are preloaded with your app and are stored in the 'res' directory, if you're downloading them from online or they're stored on the filesystem the way you load the bitmap will be somewhat different. I haven't compiled this code so it may have some bugs but this is roughly what you want to do.
I'm using Delphi7 and I'd like to have a ComboBox with separator items (Just like in popup menus).
I've seen this beautifully implemented in Mozilla Sunbird (I know, it's not Delphi...) the following way:
The separator item is a simple gray line
drawn in the center of the item
If you hover over the separator with
the mouse, the selection doesn't
appear
If the user clicks the separator,
it's not selected either AND the
combobox doesn't closeup.
No. 1 could be implemented using DrawItem. I could live without No. 2 because I have no idea about that.
For No. 3 I'm asking for your help. I've figured out that straight after closing up a CBN_CLOSEUP message is sent to the combobox.
I thought about hooking the window proc and if CBN_CLOSEUP is sent to a certain combobox then countering it. But I'm unsure if this is the best solution, or maybe there are other, more elegant ways?
Whatever the solution is, I'd like to have a standard ComboBox which supports WinXP/Vista/7 theming properly.
Thanks!
Edit: For a working component please see this thread:
Can you help translating this very small C++ component to Delphi?
I played around with making unclickable separator items (as described in this answer) and ran into several UI glitches. The problem is that combo boxes have several aspects to their behavior that can be hard to get exactly right:
Pressing the up and down arrow keys navigates the list while the list is dropped down.
Pressing Enter closes the dropped down list, selecting the current item.
Pressing Escape closes the dropped down list, selecting the current item (if the current item was chosen with the up and down arrow keys) or the last selected item.
If the combo box has the focus, then pressing the up and down arrow keys to changes the current selection without displaying the list.
If the combo box has the focus, then typing anything selects the combo box item matching whatever is typing.
If the combo box has the focus, then pressing F4 drops down the combo box list, which can then be controlled by keyboard or mouse.
Ensuring that disabled separator items don't respond to any of these events (plus any other events which I may be missing, e.g., screen readers?) seems fraught with error.
Instead, the approach I'm using is to draw the separator as part of the item:
Use a variable height owner draw combo box.
Add 3 pixels to the height for any items that need separators.
Draw a horizontal line at the top of each item needing a separator.
Here's some C++Builder code to accomplish this; translating it to Delphi should be easy enough.
void __fastcall TForm1::ComboBox1DrawItem(TWinControl *Control,
int Index, TRect &Rect, TOwnerDrawState State)
{
bool draw_separator = NeedsSeparator(Index) &&
!State.Contains(odComboBoxEdit);
TCanvas *canvas = dynamic_cast<TCustomCombo*>(Control)->Canvas;
canvas->FillRect(Rect);
TRect text_rect = Rect;
// Add space for separator if needed.
if (draw_separator) {
text_rect.Top += 3;
}
canvas->TextOut(text_rect.Left + 3,
(text_rect.Top + text_rect.Bottom) / 2 -
canvas->TextHeight(ComboBox1->Items->Strings[Index]) / 2),
ComboBox1->Items->Strings[Index]);
// Draw a separator line above the item if needed.
if (draw_separator) {
canvas->Pen->Color = canvas->Font->Color;
canvas->MoveTo(Rect.Left, Rect.Top + 1);
canvas->LineTo(Rect.Right, Rect.Top + 1);
}
}
void __fastcall TForm1::ComboBox1MeasureItem(
TWinControl * /* Control */, int Index, int &Height)
{
Height = ComboBox1->ItemHeight;
// Add space for the separator if needed.
if (Index != -1 && NeedsSeparator(Index)) {
Height += 3;
}
}
What you want is an owner-drawn combobox. See this: http://delphi.about.com/od/vclusing/a/drawincombobox.htm
Also, this seems to solve making the item unclicable:
http://borland.newsgroups.archived.at/public.delphi.vcl.components.using.win32/200708/0708225320.html
As far as I know there is no VCL way of doing that, so you'll have to subclass the combobox. It would be nice to create component encapsulating those functionalities so you can reuse them easily.
God bless
If you want your controls to look good use the free SpTBXLib. It supports combo style components which popup a popup menu with lines.