I'm opening a new flex project in Flash builder and I'm trying to make an interface with 3 buttons, "cat", "dog", and "bird." When each of the buttons are pressed, I would like an image box on the bottom of the screen to fill in with the appropriate animal's image.
I'm having a lot of trouble figuring out how to display an image in AS3. Can anyone help point me in the right direction?
One of the simplest way would be, if you don't mind your swf to embed the three images:
embed code
[Embed(source="/../assets/cat.jpg")]
static private const Cat:Class;
[Embed(source="/../assets/dog.jpg")]
static private const Cat:Class;
[Embed(source="/../assets/bird.jpg")]
static private const Cat:Class;
container initialization
private var imageBox:Sprite = new Sprite;
// initialize imagebox position/chose parent here, for instance:
container.addChild(imageBox);
cat button click handler
imageBox.removeChildren();
imageBox.addChild(new Cat);
dog button click handler
imageBox.removeChildren();
imageBox.addChild(new Dog);
bird button click handler
imageBox.removeChildren();
imageBox.addChild(new Bird);
I'm assuming your project has "assets" & "src" directories, containing respectively your images and your as3 code
Related
I am using this binding for this awesome Material Drawer Library by MikePenz.
I have implemented the Navigation Drawer with this library and I have also managed to change the hamburger menu to the back arrow when I go level deep. Now I have some problems to get the back arrow to work correctly. When I click on the back arrow, rather than going back to the previous page, it opens up the navigation drawer.
After looking into the original library, I have identified, the following code is responsible to manage the back arrow button. I would appreciate , if someone can help me a bit to write this listener code in C#.
.withOnDrawerNavigationListener(new Drawer.OnDrawerNavigationListener() {
#Override
public boolean onNavigationClickListener(View clickedView) {
//this method is only called if the Arrow icon is shown. The hamburger is automatically managed by the MaterialDrawer
//if the back arrow is shown. close the activity
AdvancedActivity.this.finish();
//return true if we have consumed the event
return true;
}
})
Here is the binding libray that I use : MaterialDrawer-Xamarin
And this is the link to the original Library : MaterialDrawer
Try something like this:
var result = new DrawerBuilder()
.WithActivity(this)
.AddDrawerItems(
//Add some items here
new DividerDrawerItem()
)
.WithOnDrawerNavigationListener(this);
and implement Drawer.IOnDrawerNavigationListener in your activity like this:
public bool OnNavigationClickListener(View clickedView)
{
this.Finish();
return true;
}
I am developing an iPad app using Xamarin.Forms.
I would like my settingspage to be modal so it lay over the previous page like a popup.
I have been trying all solutions I could find and most of them seems to recommend me to call this from a button:
await Navigation.PushModalAsync(ModalSettingsPage);
What happens when I use it is that my settingspage comes in from below as a modal page but not as a popup, it covers the entire screen.
This is my current code:
//Setup button (action bar)
ToolbarItems.Add(new ToolbarItem
{
// Text = "Setup",
Icon = "settings1.png",
Order = ToolbarItemOrder.Default,
Command = new Command(() => Navigation.PushModalAsync(new ModalSettingsPage())) //Action to perfome on click , open modal view
});
Also, does anyone now if there is any good way to positions the ToolbarItems? I have two items and would like each one to be positioned at each side, but by default they are both positioned to the right.
With the evolution of Forms (currently 4.5.0), it has now become simple to push a modalpage which is not fullscreen. Use the following in your code-behind of your xaml contentpage:
using Xamarin.Forms;
using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.iOSSpecific;
namespace YourAppWithAModalPage
{
public class ModalPage : ContentPage
{
public ModalPage()
{
// iOS Solution for a ModalPage (popup) which is not fullscreen
On<iOS>().SetModalPresentationStyle(UIModalPresentationStyle.FormSheet);
}
}
}
There is nothing pre-made in Forms to give you a popup like I think you want. Instead, you would have to create this or use a third-party solution. For example, here is a "popup" implementation that might work for you.
I'm following this tutorial on youtube about changing sprite animation in code, and I was wondering if I could change this to changing sprite animation using UI button. does anyone knows how to do this. Thank you!
EDIT:
The script that I reposed kind of works thanks to your help, it changes the sprite image from image one to image two but what I'm basically trying to achieve is that each time that I click the UI button the sprite image will change from sprite image one (UI button click)> sprite image two (UI button click)> sprite image three (UI button click)> then repeat the process instead of the sprite image automatically changing itself.
Buttons have an OnClick event http://docs.unity3d.com/ScriptReference/UI.Button-onClick.html
You just create a method that gets called when the button is clicked, in your case the changing sprite code. Seen as you are using a timer though you will need to use something like a bool because onClick() only gets called once when clicked, not every frame.
Look https://www.youtube.com/watch?v=J5ZNuM6K27E
bool b_RunSpriteAnim;
public void onClick(){
b_RunSpriteAnim = true;
}
void Update(){
if (b_RunSpriteAnim)
//your anim sprite stuff
}
Then once the sprite anim has finished, just toggle b_RunSpriteAnim to false and reset the timer.
Edited:
You don't need a boolean. I only thought you wanted it because you were using a timer (as based on the Youtube link). If you just want to change the sprite immediately then you do not need it. As for Imagethree not working, it's because you have never included it in your code. It isn't clear what you are trying to achieve with Imagethree, if you included this in onClick as well it would just overwrite the image two that was just set, so I am not sure what you are looking to achieve.
public void onClick(){
this.gameObject.GetComponent<SpriteRenderer>().sprite = Imagetwo;
}
Second Edit:
public Sprite[] Images;
//Index starts at one because we are setting the first sprite in Start() method
private int _Index = 1;
void Start(){
//Set the image to the first one
this.gameObject.GetComponent<SpriteRenderer>().sprite = Images[0];
}
public void onClick(){
//Reset back to 0 so it can loop again if the last sprite has been shown
if (_Index >= Images.Length)
_Index = 0;
//Set the image to array at element index, then increment
this.gameObject.GetComponent<SpriteRenderer>().sprite = Images[_Index++];
}
I'm trying to figure out how to realize the HUD interface for my brand new game.
I wanted to create it by using the scene2d.ui set of widgets because I've heard that it's more suitable for a responsive UI.
So, I'm trying to create a simple HUD in which there's a score Label aligned at the top-right side of the screen and a pause Button placed on the top-left side of the screen.
Now... I'm using a root Table (as explained in the Table guide https://github.com/libgdx/libgdx/wiki/Table#quickstart) that fills the stage.
I wanted to create a custom class for handling all the hud interface such as:
public class Hud extends Table{
private Label scoreLabel;
private Button pauseButton;
public Hud(){
setFillParent(true);
BitmapFont font = (BitmapFont) MyGame.assetManager.get(Constants.COUNTERS_FONT_NAME);
LabelStyle labelStyle = new LabelStyle(font,Color.WHITE);
scoreLabel = new Label("score:0", labelStyle);
add(scoreLabel).width(2).height(1);
}
}
The issue I'm fighting with is the following: the text inside the Label does not stay inside the Label.
I mean: activating the debug mode I can see the rectangle representing the Label with the correct dimensions...but the text is completely out of bounds and does not scale.
As you can see the font I use is a BitmapFont that is generated when the Game starts via the library FreeTypeFont...The font is then added to the AssetsManager of the game.
I post the code I use for adding the font generator to the assets manager:
//Register the FreeTypeFontGeneratorLoader
InternalFileHandleResolver resolver = new InternalFileHandleResolver();
assetManager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
assetManager.setLoader(BitmapFont.class, ".ttf", new FreetypeFontLoader(resolver));
//Load fonts
FreeTypeFontLoaderParameter countersFontParameters = new FreeTypeFontLoaderParameter();
countersFontParameters.fontFileName = Constants.BASE_FONT_NAME;
countersFontParameters.fontParameters.size = 45;
countersFontParameters.fontParameters.borderColor = Color.BLACK;
countersFontParameters.fontParameters.borderWidth = 1;
assetManager.load(Constants.COUNTERS_FONT_NAME, BitmapFont.class, countersFontParameters);
As you can see I load just one BitmapFont with a size of 45 pixels.
I don't know what the problem is; I would like to try with a classic BitmapFont .fnt in order to see if the problem is in the way the BitmapFont is generated but I'd also like to keep the FreeTypeFont solution.
Thanks in advance,
Luca
P.S.
One thing about the Stage... it uses a ScalingViewport so the dimensions are not in pixels but in virtual units (because my Game uses Box2d and I need a common unit both for the rendering and the physic simulation).
I'm pretty sure that Label will not respond on automatic scaling of the viewport until you will call label.setFontScale( theScale ) explicite.
The best idea here, in my opinion, will be to create another stage exclusively for a HUD so you will have your stage with ScalingViewport (I admit I've never used this one - isn't it better to use some Fill or aomething like this one?) and another stage with FitViewport above for example.
I find it very reasonable and intuitive since it will be like having your HUD displayed on yours car windscreen when driving the car.
The code should looks like:
//in your show method
viewport = createYourScalingViewport();
stage = new Stage();
stage.setViewport(viewport);
HUDviewport = new FitViewport(screenWidth, screenHeight);
HUDstage = new Stage();
HUDstage.setViewport(HUDviewport);
...
//adding some actor to stages
stage.addActor(actor1);
stage.addActor(actor2);
//BUT
HUDstage.addActor( yourHudInstance );
...
//then in your render method act() and draw() stage then HUDstage so the HUD will be above stage
stage.act();
stage.draw();
HUDstage.act();
HUDstage.draw();
Remember of updateing HUDviewport in case of window resizing in the resize Screen method
I guess I've solved my problem.
It seems to be that it doesn't make sense to set the label dimensions.
If anyone needs to have a container that was tight to the text is rendered inside the label the best solution is to create a custom class like this below that extends an HorizonaltGroup (o VerticalGroup).
Let's imagine I need a score label for my game:
public class ScoreLabel extends HorizontalGroup{
...
}
Inside this class I create a Label property and I add it as an Actor.
public class ScoreLabel extends HorizontalGroup{
private Label scores;
public static final float FONT_SCALE = 0.9f;
public ScoreLabel(){
BitmapFont font = MyGame.assetManager.get(Constants.COUNTERS_FONT_NAME);
LabelStyle labelStyle = new LabelStyle(font, Color.WHITE);
scores = new Label(getScoreText(), labelStyle);
scores.setAlignment(Align.left);
scores.setFontScale(FONT_SCALE);
addActor(scores);
}
}
If you activate the debug mode you'll notice that there's a container that is perfectly tight to the text is rendered.
The code above simply loads a BitmapFont previously loaded inside the game assets manager.
Then a font scale is applied...I guess it could be added inside a skin property
Anyway...This is the best solution for me.
I'm still using a stage with a ScalingViewport, in this way I have not to use pixels as unit and all the dimensions are scaled to fill the device screen.
Bye,
Luca
I want to make a drop down menu with images. This means:
ImageListName
image.png
image1.png
......
imageN.png
There should be only images no text and I want to select on image like when you can choose an avatar.
I'm not sure which approach is the best.
MenuBar HomeMenu = new MenuBar();
final String image = "<img src='"+GWT.getModuleBaseURL() + "/images/down-arrow.png' height='25px' width='25px'/>";
SafeHtml addActivityImagePath = new SafeHtml() {
#Override
public String asString() {
return image;
}
};
HomeMenu.addItem(new MenuItem(addActivityImagePath,mainMenu));
Take a look at the Combobox or Suggestion box in Advanced GWT Components.
You won't be able to do this with a ListBox, because it just creates an HTML < select> element. You can use a MenuBar that has one menu with MenuItems in it to simulate a dropdown with complex widgets inside it. You will also be able to style the dropdown rather than rely on browser-styled form elements.use MenuBar instead of ListBox and place any widget you want inside the MenuItem to simulate a ListBox. Regular ListBoxes will only allow you to specify plain text.