当应用处于背景状态时,无法暂停游戏

时间:2016-05-01 19:09:12

标签: objective-c uiapplicationdelegate uiapplication

这可能是一个非常基本的问题,但我做了一个简单的游戏,用户可以避免遇到障碍。当我按下主页按钮时,我的应用程序仍然在后台运行,因此应用程序音乐继续播放,我的所有NSTIMERS继续播放,最终导致游戏角色遇到障碍。)

当我按下主页按钮或切换到另一个应用程序(游戏处于后台状态)时,可以告诉我如何暂停游戏,然后当应用程序再次出现在前台时恢复?

2 个答案:

答案 0 :(得分:1)

我不知道你是如何做你的代码的,但是当用户将应用程序放在后台时你可以在AppDelegate.m中使用这个方法来了解确切的时刻:

namespace SomeApp
{
    [Activity (Label = "App")]          
    public class MapActivity : Activity
    {
        protected override void OnCreate (Bundle savedInstanceState)
        {
            base.OnCreate (savedInstanceState);
            SetContentView(Resource.Layout.MapLayout);

            MapLocationSetup ();

        }

        public void MapLocationSetup () {

            string currentLocationLatitude = Intent.GetStringExtra ("currentLocationLatitude");
            string currentLocationLongitude = Intent.GetStringExtra ("currentLocationLongitude");

            var location = new LatLng(currentLocationLatitude, currentLocationLongitude);
            CameraPosition.Builder builder = CameraPosition.InvokeBuilder();
            builder.Target(location);
            builder.Zoom(18);
            builder.Bearing(155);
            builder.Tilt(85);
            CameraPosition cameraPosition = builder.Build();
            CameraUpdate cameraUpdate = CameraUpdateFactory.NewCameraPosition(cameraPosition);

            MapFragment mapFrag = (MapFragment) FragmentManager.FindFragmentById(Resource.Id.mapLayout);
            GoogleMap map = mapFrag.Map;
            if (map != null) {
                map.MoveCamera(cameraUpdate);
                //map.MapType = GoogleMap.MapTypeSatellite;
            }
        }
    }
}

在方法内部,您可以将音乐设置为停止并设置所有NSTIMERS。你可以使用这个:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

    NSLog(@"going to background");
}

知道用户何时从后台返回前台。

答案 1 :(得分:0)

很简单......

使用此代码:

using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            //Application.Exit(); //NoOp
        }
        //Loop to show this isn't dependent on the total number of times
        //Application.Exit() is called.
        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                Application.Exit();
            }
        }
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            Debug.WriteLine("OnFormClosing called.");
            base.OnFormClosing(e);
        }
        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            Debug.WriteLine("OnFormClosed called.");
            base.OnFormClosed(e);
        }
    }
}

将它放在包含定时器和方法的.m文件的setup方法中。




也可以将它放在.m文件中

    [[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(pauseGameSoUserDoesNotDieForNoReason:)
                                             name: UIApplicationDidEnterBackgroundNotification
                                           object: nil];

最后一种方法:
-(void)pauseGameSoUserDoesNotDieForNoReason:(NSNotification*)theNotification{ }
是你放置暂停代码的地方。

注意:

此代码可以放在几个.m文件中。