如何从奖励视频广告中奖励kivy中的用户积分

How to reward user points in kivy from rewarded video ads

我正在尝试解决 Weeks 的问题,最后我不得不在这个网站上提问。

问题:-

当用户播放视频广告时,积分会增加,用户会得到奖励。但是当用户用积分购买东西时,积分并没有减少。

据我了解,这是一个面向对象的问题。

查看此代码片段:

Python代码:

class PlayScreen(Screen):

# Function for display sneak peak (Ad Advantage)
    def Purchase_For_points(self):       #This FUnction will be called when user purchases some hint
        // Irrelevant Code
        app = MDApp.get_running_app()
        app.Num -= 1                     #After Purchase Deducted 1 Point

    def VideoAdPopup(self):
        video_dialog = MDDialog(title = "Watch an Ad",text = "Watch a small video Ad In Exchange of Reward",
                       size_hint = [1,0.3],auto_dismiss = False,text_button_ok = "Ok",text_button_cancel = 
                       "Cancel",events_callback = self.videocallback)

        video_dialog.open()

    def videocallback(self,text_of_Selection,popup_widget):  #Popup Function for rewarded ads
        if(text_of_Selection == "Ok"):
            SlipsApp.ads.show_rewarded_ad()     #Ad is Shown and rewards will be handled accordingly
        else:
            print("User did not want to watch ad")


class SlipsApp(MDApp):

    # These are our Admob Ad IDs
    APP = "ca-app-pub-XXXXXXXXXXXXXXXXXXXX"
    BANNER = "ca-app-pub-XXXXXXXXXXXXXXXXXXXX"
    INTERSTITIAL = "ca-app-pub-XXXXXXXXXXXXXXX"
    REWARDED_VIDEO = "ca-app-pub-XXXXXXXXXXXXXXXXX"
    TEST_DEVICE_ID = "3C91BAC5C088815B62389497AC1E309D"
    # Creating Ad Instance
    ads = KivMob(APP)

    #Number oF Rewards(Points)
    Num = NumericProperty(5)       #Variable for points

    def __init__(self, *args,**kwargs):
        self.theme_cls.theme_style = "Dark"
        super().__init__(*args,**kwargs)
        self.reward = Rewards_Handler(self)

    #Build Function
    def build(self):
        # Loading Ads
        self.ads.add_test_device(self.TEST_DEVICE_ID)
        self.ads.new_banner(self.BANNER, False)
        self.ads.new_interstitial(self.INTERSTITIAL)
        self.ads.request_banner()
        self.ads.set_rewarded_ad_listener(self.reward)
        self.ads.load_rewarded_ad(self.REWARDED_VIDEO)
        self.ads.show_banner()
        return intro()

#Class For Handling Rewards Callback Functions
class Rewards_Handler(RewardedListenerInterface):
    def __init__(self,other):
        self.game = other

    #Overriding Rewards Callback Functions
    def on_rewarded(self, reward_name, reward_amount):
        self.game.Num += 1                  #Reward given 1 Point
        print("User Given 1 reward")

基维代码:-

<PlayScreen>:
    name: 'PlayScreen'

    MDLabel:
        text:"  Points : "+str(app.Num)    #Showing Points On Screen
        pos_hint:{"top":1.35}

是的,你是对的,这是一个对象问题。在 PlayScreen class 中的函数 Purchase_For_points() 中。您再次创建用于访问 App class.This 的对象将复制新对象中的所有属性并在 运行 App.

的新对象中每次减少 Num

而且,屏幕上显示的是在应用程序启动时创建的主应用程序对象的 Num 变量。 因此,要减少和增加 Num 变量,您必须有权访问主应用程序对象。

在您的代码中,我看到您已通过 init 方法将 App Class 的访问权限授予 Rewards_Handler class。 所以在 Rewards_Handler class 游戏对象 主应用程序对象 .

相同

在 Rewards_Handler class 中创建一个不同的函数来减少积分,因为这是处理奖励相关事情的 class

class Rewards_Handler(RewardedListenerInterface):
    def decrementReward(self):
        self.game.Num -= 1

要从不同的 classes 访问此函数,您可以在 App 中创建一个函数 Class 它必须是静态方法,这样您就不会创建另一个对象并陷入对象中

class SlipsApp(MDApp):
    @staticmethod
    def decrement(element): #The element will be the main app object.
        element.reward.decrementReward()

现在获取对 class 的访问权限,您希望在其中使用此函数进行递减。 只需在 class

中添加一个 init() 方法
class PlayScreen(Screen):
    def __init__(self, **kw):
        super().__init__(**kw)
        self.app = MDApp.get_running_app() #this is main app object

    def Purchase_For_points(self):
        SlipsApp.decrement(self.app) #passing main App object.